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 Question

Author  Topic 

eembme
Starting Member

18 Posts

Posted - 2007-02-23 : 12:28:23
Site number Patient number
1 1
1 2
1 3
2 1
2 2
3 1
3 2

How do I count the number of unique patients per unique site number
site 1 would have 3 patients
site 2 would have 2 patients
site 3 would have 2 patients

Thanks!

X002548
Not Just a Number

15586 Posts

Posted - 2007-02-23 : 12:30:46
SELECT [Site number], COUNT(*) AS CoyuntOfPatients
FROM myTable99
GROUP BY [Site Numer]


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

eembme
Starting Member

18 Posts

Posted - 2007-02-23 : 12:36:02
That does not work because there are a bunch of readings per patient. Even when I use the DISTINCT function, it still counts all of the readings per patient. I was thinking that I would need some type of subquery to do it, but I can't seem to develop one that does not pull all the readings, not just the patient number.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-02-23 : 13:19:52
Did you use DISTINCT the proper way?

SELECT [Site number], COUNT(DISTINCT [Patient Number]) AS CountOfPatients
FROM myTable99
GROUP BY [Site Number]


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

eembme
Starting Member

18 Posts

Posted - 2007-02-23 : 13:22:13
Thank you, I was grouping by both
Go to Top of Page
   

- Advertisement -