SQL / Hive如何将两个不同的查询合并到一个具有不同列的结果中

时间:2018-07-10 23:31:55

标签: sql hive

对不起,标题太烂了。 我想将这两个查询基本上都转换为一个查询,结果有两列:

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

1 个答案:

答案 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);