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)
 group by and count of each group

Author  Topic 

rtutus
Aged Yak Warrior

522 Posts

Posted - 2007-10-23 : 11:44:56
Hi,
I use group by myItem, that displays rows for each value of myItem.
I want at the end of each group for each specific myItem, I want an extra row to tell me the count of the rows for that group.

How do i do that please. Here s what I need, for example for this set of rows:


myItem colum2 colum3
711056 22662 Finances / -2291 -2479916
711056 22663 Finances / -2380 -2576255
711056 43428 Cda) -323 -349635
711056 43428 . -B-(Cda) -44 -47628
711057 44348 . -B-(Cda) -355 -384273
711057 47033 . -B-(Cda) -1278 -1383384


I need 2 extra rows, one at the end of the items 711056 telling me there are 4 rows for the item 711056 and one row at the very end telling me there are: 2 rows for the item: 711057

Thanks a lot.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-23 : 12:04:28
COUNT(*) OVER (PARTITION BY MyItem)



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

stonebreaker
Yak Posting Veteran

92 Posts

Posted - 2007-10-23 : 12:13:50
You could do this a couple of ways. I would probably declare a cursor for distinct values of myItem, then do two select statements:

select myItem, column2, column3
where myItem = value of cursor

select myItem, count(*)
where myItem = value of cursor

fetch next from cursor
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-24 : 03:11:40
quote:
Originally posted by stonebreaker

You could do this a couple of ways. I would probably declare a cursor for distinct values of myItem, then do two select statements:

select myItem, column2, column3
where myItem = value of cursor

select myItem, count(*)
where myItem = value of cursor

fetch next from cursor


Did you read Peso's reply?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -