views:

48

answers:

2

Total records in table are 10.

Select count(ID) from table1 where col1 = 1 (Result is 8)
Select count(ID) from table1 where col1 = 0 (Result is 2)

So its a same table but count is based on different condition. How am i gonna get two results (counts) using one select statement?

Also Performance is a big concern here.

PS: I am using Stored procedure...

EDIT: I wanna clear the above query is just a part of a big SP logic (for me at least). Since i got these following answers, it gave another idea to achieve it in different way. My above question is a bit changed now.....Please help here? Its a same col (bool type) with true or false state.

A: 

You should use subselects or UNIONS, I don't see the other way...

Daria Barteneva
+5  A: 

Use CASE:

SELECT
    SUM(CASE col1 WHEN 1 THEN 1 ELSE 0 END) AS Count1,
    SUM(CASE col1 WHEN 0 THEN 1 ELSE 0 END) AS Count0
FROM table1
Daniel Renshaw
+1 that's how I'd do it
KM
Agree with @KM. How do i do it?
Novice
My question is changed a bit..please read again..
Novice
I've updated the answer in line with your revised question
Daniel Renshaw
Sum() Vs Count() what do u suggest in terms of performance?
Novice
For this CASE approach, you'd have to use SUM.
Daniel Renshaw