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
 counts in SQL

Author  Topic 

albertkohl
Aged Yak Warrior

740 Posts

Posted - 2010-04-14 : 15:16:02
got a question. i have a table of client information, and inside that we have a column flagging the client as "SPANISH" or not...

is there a way to run counts w/o having to make multipule passes, and at the same time not writing it into a temp table?

for example, this is what i'm doing now:


select ST,count(*) as 'Count' into #counta from ##temp group by st order by st
select ST,count(*) as 'HISPANIC' into #countb from ##temp where spanish = 'y' group by st order by st

select a.ST,COUNT,hispanic from #counta a join #countb b on b.st = a.st
group by a.st,COUNT,hispanic


i tried something like this, but i know it doesnt work:

select st,COUNT(st),(case spanish when 'Y' then COUNT(st) end) from ##temp group by st order by st

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2010-04-14 : 15:20:51
select st, count(*), sum(case when spanish = 'Y' then 1 else 0 end)
from ##temp group by st

quote:
Originally posted by albertkohl

got a question. i have a table of client information, and inside that we have a column flagging the client as "SPANISH" or not...

is there a way to run counts w/o having to make multipule passes, and at the same time not writing it into a temp table?

for example, this is what i'm doing now:


select ST,count(*) as 'Count' into #counta from ##temp group by st order by st
select ST,count(*) as 'HISPANIC' into #countb from ##temp where spanish = 'y' group by st order by st

select a.ST,COUNT,hispanic from #counta a join #countb b on b.st = a.st
group by a.st,COUNT,hispanic


i tried something like this, but i know it doesnt work:

select st,COUNT(st),(case spanish when 'Y' then COUNT(st) end) from ##temp group by st order by st


Go to Top of Page

albertkohl
Aged Yak Warrior

740 Posts

Posted - 2010-04-14 : 15:31:20
wow... why didnt i think of that...
Go to Top of Page

albertkohl
Aged Yak Warrior

740 Posts

Posted - 2010-04-14 : 15:31:29
thanks sooo much!
Go to Top of Page
   

- Advertisement -