对不起,标题太烂了。 我想将这两个查询基本上都转换为一个查询,结果有两列:
select count(columnA) as prev from myTable where set_id = 1530880217;
select count(columnA) as curr from myTable where set_id = 1530901756;
输出:
prev | curr
1000 | 1500
答案 0 :(得分:1)
使用条件聚合:
select sum(case when set_id = 1530880217 then 1 else 0 end) as prev,
sum(case when set_id = 1530901756 then 1 else 0 end) as curr
from myTable
where set_id in (1530880217, 1530901756);
这假设columnA
从未为NULL
。如果您确实要NULL
进行检查:
select sum(case when set_id = 1530880217 and ColumnA is not null then 1 else 0 end) as prev,
sum(case when set_id = 1530901756 and ColumnA is not null then 1 else 0 end) as curr
from myTable
where set_id in (1530880217, 1530901756);