Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 running count based on group of data

Author  Topic 

sivakrishna546
Starting Member

1 Post

Posted - 2011-10-08 : 17:04:47
Hi Guys,

I have a requirement. Data in the table is as follows.

col1 col2 TRNSCTN_CD LOAD_LOG_KEY

2324 22345 I 1400001
1435 23145 I 1400001
1324 11235 I 1400001
1232 12324 U 1500001
1253 23132 I 1500001
4254 90034 I 1500001
8764 29405 I 1600001


I need output like :

1400001 3 /*(count of 1400001)*/
1500001 6 /*(count of 1400001 + 1500001)*/
1600001 7 /*(count of 1400001+1500001+1600001)*/

I'm using group by but getting the count for only particular load_log_key count.

Could any one please throw somelight on this problem.

Thanks in Advance
Siva Krishna

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-09 : 12:58:06
[code]
SELECT LOAD_LOG_KEY,RunCnt
FROM (SELECT DISTINCT LOAD_LOG_KEY FROM Table) t
CROSS APPLY (SELECT COUNT(*) AS RunCnt
FROM Table
WHERE LOAD_LOG_KEY<= t.LOAD_LOG_KEY) t1
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -