SQL/Hive如何将两个不同的查询组合成一个具有不同列的结果
如果标题不好,我很抱歉。 我想基本上将这两个查询转换为一个查询,结果有两列:SQL/Hive如何将两个不同的查询组合成一个具有不同列的结果,sql,hive,Sql,Hive,如果标题不好,我很抱歉。 我想基本上将这两个查询转换为一个查询,结果有两列: 从myTable中选择count(columnA)作为prev,其中set_id=1530880217 从myTable中选择count(columnA)作为curr,其中set_id=1530901756 输出: prev | curr 1000 | 1500 使用条件聚合: select sum(case when set_id = 1530880217 then 1 else 0 end) as prev,
从myTable中选择count(columnA)作为prev,其中set_id=1530880217代码>
从myTable中选择count(columnA)作为curr,其中set_id=1530901756代码>
输出:
prev | curr
1000 | 1500
使用条件聚合:
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);
如果在(15308802171530901756)@MrMoose中设置该id。非常感谢。