Sunday, March 11, 2012

A small COUNT() question

Hello,

I have a table which contains a boolean column. I need to write a query which returns a count() of all the TRUE occurences and a count() of all the FALSE occurences in this table, in 2 different columns in the result table.

Can it be done?

SELECT (SELECT COUNT() FROM TABLEx WHERE FIELDx=1) as CTrue,

(SELECT COUNT() FROM TABLEx WHERE FIELDx=0) as CFalse

|||

you should be using following SQL

select sum(case when field = 1 then 1 else 0 end) true, sum(case when field = 1 then 0 else 1 end) false

from table x

In this way, you can gain more performance

No comments:

Post a Comment