我有以下查询:
'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'
是否可以将这两个查询合并为一个?谢谢!
答案 0 :(得分: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
正如Edamame所说,你可能想要阅读每个团队的两个结果。问题本身并不清楚,但可以通过这种方式解决:
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
答案 1 :(得分:0)
如果你认为返回的结果不同,你应该使用“UNION ALL”,因为你只使用“UNION”,sql会区分结果以影响查询的性能