Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Aggregate function for Boolean I have a table that flags rows with a Boolean. I need to count the number of rows with a 1 and the number of rows with a 0. The following SELECT counts properly but I don’t know what aggregate function to use so I can tell which count is for the 1’s and which is for the 0’s. What function should I use? Or should I be doing this a different way?select count(*), sum(UniqueName) from MyTable group by UniqueName
TG
Master Smack Fu Yak Hacker
6065 Posts
Posted - 2008-01-27 : 19:28:14
Does this work for you?
declare @t table (rowid int identity(1,1), b bit)insert @t (b)select 1 union all select 1 union all select 0 union all select 0 union all select 0select * from @tselect b, count(*) bCount from @t group by boutput:--select * from @trowid b ----------- ---- 1 12 13 04 05 0--select b, count(*) bCount from @t group by bb bCount ---- ----------- 0 31 2