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 2000 Forums
 Transact-SQL (2000)
 counting confusion

Author  Topic 

elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2003-09-30 : 03:53:57
I'm not even clear that this is possible - but hey, I'll ask anyway.

I want to produce two counts, one with a condition on it

So for example, my people table monitors ethnicity so

SELECT ethnicity, count(*) from PEOPLE group by ethnicity

will give me one of the columns I want. e.g.

ethnicity count
Unknown 200
Not stated 5000


However I want to be able to compare this with the people with a certain condition, so

SELECT ethnicity, count(*) from PEOPLE where id in select id from OtherTable group by ethnicity

will give me this e.g.

ethnicity count
Unknown 150
Not stated 200

What I want is to combine them into a single query so that It is easier to show the comparison and I can then analyse them e.g. show the differences etc

so I would get

ethnicity count count(with condition)
Unknown 200 150
Not stated 5000 200

It sounds very simple but I can't get it to work!

thanks


steve




Steve no function beer well without

Stoad
Freaky Yak Linguist

1983 Posts

Posted - 2003-09-30 : 04:06:01
SELECT
ethnicity,
count(*),
sum(case when yourCondition then 1 else 0 end)

from PEOPLE where id ....

group by ethnicity
Go to Top of Page

elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2003-09-30 : 08:23:41
D'OH!!

Thanks very much

steve

Steve no function beer well without
Go to Top of Page
   

- Advertisement -