I am trying select multiple conditional summaries into a single table result on a DB2 based database.
Example:
SELECT COUNT(FOO) FROM T1 WHERE T1.A=1 AS A_COUNT,
SELECT COUNT(FOO) FROM T1 WHERE T1.B=2 AS B_COUNT
Ext...
Any help is appreciated.
I am trying select multiple conditional summaries into a single table result on a DB2 based database.
Example:
SELECT COUNT(FOO) FROM T1 WHERE T1.A=1 AS A_COUNT,
SELECT COUNT(FOO) FROM T1 WHERE T1.B=2 AS B_COUNT
Ext...
Any help is appreciated.
select count(case when t1.a = 1 then foo else null end) as A_COUNT
, count(case when t1.b = 2 then foo else null end) as B_COUNT
from t1
where t1.a = 1
or t1.b = 2
Where clause is optional strictly speaking but may assist performance. Also "else null" is implicit when the else clause is omitted so you can safely leave that off as well.