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
 General SQL Server Forums
 New to SQL Server Programming
 Count and grouping

Author  Topic 

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2011-09-27 : 12:24:29
Hi

How do I show the count of each grouping in the same row as each counted value, like this:


Animal Count
====== =====
CAT 3
CAT 3
CAT 3
DOG 2
DOG 2


It's like I need to do a count and grouping then use that against the original dataset without the grouping, I take it there would be a sub query with a self-join or similar, I am not sure?

Thanks

G

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-27 : 12:27:56
[code]SELECT Animal,COUNT(1) OVER (PARTITION BY Animal) AS [Count]
FROM table
[/code]

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

Go to Top of Page
   

- Advertisement -