【问题标题】:SQL/HIVE How to combine two queries with different where statements into one query?SQL/HIVE 如何将两个具有不同 where 语句的查询组合成一个查询?
【发布时间】:2018-07-18 14:04:53
【问题描述】:

我有以下查询,我试图计算 data_set “1234”的 columnA 为 Y 或 N 的所有实例。

select
data_set as DATA_SET,
sum(case when columnA <> '' and columnA = ‘Y’ then 1 else 0 end) as YES,
sum(case when columnA <> '' and columnA = ’N’ then 1 else 0 end) as NO
from myTable1
where data_set = 1234
group by data_set;

返回:

data_set       | YES           | NO
1531587375     | 6,287,732     | 54,228,649

现在,我现在想做的是如何在同一个查询中查找两个不同的 data_set (ds1=1234, ds2=4321) 并返回如下内容:

Column |  ds1.count | ds2.count
Y      |  134,543   | 153,678
N      |  5,080     | 989,820

【问题讨论】:

  • 多个数据集与您的是/否计算有何关系?

标签: sql hive


【解决方案1】:

我想我明白了,以下对我有用:

select
columnA,
sum(case when columnA <> '' and data_set = 1234 then 1 else 0 end) as PREVIOUS_COUNT,
sum(case when columnA <> '' and data_set = 4321 then 1 else 0 end) as CURRENT_COUNT
from myTable1
where data_set in (1234, 4321)
group by columnA
order by columnA ASC;

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 2015-10-08
    • 2014-03-20
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多