【问题标题】:SQL/Impala: combined multiple query (with different where clause) into oneSQL/Impala:将多个查询(具有不同的 where 子句)合并为一个
【发布时间】:2016-08-16 22:36:21
【问题描述】:

我有以下疑问:

'select team, count(distinct id) as distinct_id_count_w1 from myTable where timestamp > t1  and timestamp < t2 group by team'

'select team, count(distinct id) as distinct_id_count_w2 from myTable where timestamp > t2  and timestamp < t3 group by team'

是否可以将这两个查询合二为一?谢谢!

【问题讨论】:

    标签: sql pyspark impala


    【解决方案1】:

    很容易 :) 这应该适用于大多数常见的数据库引擎:

    select team, count(distinct id) as distinct_id_count_w1, null as distinct_id_count_w2 from myTable where timestamp > t1  and timestamp < t2 group by team
    
    UNION ALL
    
    select team, null as distinct_id_count_w1, count(distinct id) as distinct_id_count_w2 from myTable where timestamp > t2  and timestamp < t3 group by team
    

    正如毛豆所说,您可能希望阅读每个团队的两个结果。问题本身并不清楚,但可以这样解决:

    SELECT
        COALESCE(interval1.team interval2.team) AS team,
        interval1.distinct_id_count_w1,
        interval2.distinct_id_count_w2
    FROM (
        select team, count(distinct id) as distinct_id_count_w1 from myTable where timestamp > t1  and timestamp < t2 group by team
    ) AS interval1
    FULL OUTER JOIN
    (
        select team, count(distinct id) as distinct_id_count_w2 from myTable where timestamp > t2  and timestamp < t3 group by team
    ) AS interval2
    ON interval1.team IS NULL OR interval2.team IS NULL OR interval1.team = interval2.team
    

    【讨论】:

    • 但结果没有加入使用团队。一半的行具有 distinct_id_count_w2 = None,另一半具有 distinct_id_count_w1 = None。每行都应该有一个有效值 distinct_id_count_w1 和一个有效值 distinct_id_count_w2
    【解决方案2】:

    如果你认为返回的结果不同,你应该使用“UNION ALL”,因为你只使用“UNION”,sql会区分结果以影响查询的性能

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 2018-12-12
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多