SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Multiple Counts in a Row


Multiple Counts in a Row

Author
Message
mrpolecat
mrpolecat
SSChasing Mays
SSChasing Mays (626 reputation)SSChasing Mays (626 reputation)SSChasing Mays (626 reputation)SSChasing Mays (626 reputation)SSChasing Mays (626 reputation)SSChasing Mays (626 reputation)SSChasing Mays (626 reputation)SSChasing Mays (626 reputation)

Group: General Forum Members
Points: 626 Visits: 856
As everyone else has said formatting is best left to reporting tools but this will do what you want.





declare @delim1 varchar(30),@delim2 varchar(30),@delim3 varchar(30)



select @delim1 = '---------------'+ char(10) + char(13)

select @delim2 = char(10) + char(13)

select @delim3 = ','

select @delim1,facility,@delim2,sum(case when something = somethingelse then 1 else 0 end) as con1,@delim3,sum(case when something = somethingelse then 1 else 0 end) as con2,@delim3,sum(case when something = somethingelse then 1 else 0 end) as con3,@delim2,@delim1 from #temp

group by facility






sravanb
sravanb
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 49
ALZDBA's solution works perfectly... (atleast for me)

select Facility,
sum(case when condition_1 then 1 else 0 end),
sum(case when condition2 then 1 else 0 end)
from Table
group by Facility

Solution would look like this:

Facility_1 Count(for_condition_1) Count(for_condition_2) ......
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search