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)
 How to get 2 counts at a time

Author  Topic 

satish.gorijala
Posting Yak Master

182 Posts

Posted - 2010-01-15 : 07:10:59
Hi,Following is my requirement


userid ssid Fname Status
1 101 1@ 0
2 101 2@ 1
3 101 3@ 1
4 101 4@ 2
5 102 5@ 0

I want result like below

ssid, status_0_cnt, status_x_cnt
101 1 3
102 1 0

I want results groupby ssid. But one count for Zero and another count for remaining status values.



developer :)

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-01-15 : 07:17:23

select
ssid,
sum(case when status=0 then 1 else 0 end) as status_0_cnt,
sum(case when status>0 then 1 else 0 end) as status_x_cnt
from
your_table
group by ssid



Madhivanan

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

satish.gorijala
Posting Yak Master

182 Posts

Posted - 2010-01-15 : 07:22:54
Thanks Madhavan. If i want to add Fname to output..how can i write query?
quote:
Originally posted by madhivanan


select
ssid,
sum(case when status=0 then 1 else 0 end) as status_0_cnt,
sum(case when status>0 then 1 else 0 end) as status_x_cnt
from
your_table
group by ssid



Madhivanan

Failing to plan is Planning to fail



developer :)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-01-15 : 07:29:33
quote:
Originally posted by satish.gorijala

Thanks Madhavan. If i want to add Fname to output..how can i write query?
quote:
Originally posted by madhivanan


select
ssid,
sum(case when status=0 then 1 else 0 end) as status_0_cnt,
sum(case when status>0 then 1 else 0 end) as status_x_cnt
from
your_table
group by ssid



Madhivanan

Failing to plan is Planning to fail



developer :)


Post expected result

Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-15 : 12:40:08
sounds like this

select 
ssid,
Fname,
sum(case when status=0 then 1 else 0 end) over (partition by ssid) as status_0_cnt,
sum(case when status>0 then 1 else 0 end) over (partition by ssid) as status_x_cnt
from your_table

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-15 : 12:59:40
Hmmm ... I read it that Fname, being variable per ssid, and unique within the sample data, would need to be presented as comma delimited list for each ssid, or somesuch.

Are we taking bets?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-01-18 : 01:55:52
quote:
Originally posted by Kristen

Hmmm ... I read it that Fname, being variable per ssid, and unique within the sample data, would need to be presented as comma delimited list for each ssid, or somesuch.

Are we taking bets?


Wait until OP posts back

Madhivanan

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

- Advertisement -