SQL/Impala:将多个查询(具有不同的 where 子句)合并为一个
Posted
技术标签:
【中文标题】SQL/Impala:将多个查询(具有不同的 where 子句)合并为一个【英文标题】:SQL/Impala: combined multiple query (with different where clause) into one 【发布时间】: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'
是否可以将这两个查询合二为一?谢谢!
【问题讨论】:
【参考方案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会区分结果以影响查询的性能
【讨论】:
以上是关于SQL/Impala:将多个查询(具有不同的 where 子句)合并为一个的主要内容,如果未能解决你的问题,请参考以下文章
每次运行我在 SQL Impala 中使用前导函数时都会得到不同的结果