我有以下疑问:
'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'
是否可以将这两个查询合并为一个查询?谢谢!
2条答案
按热度按时间nfeuvbwi1#
如果您认为返回的结果不同,那么应该使用“union all”,因为您只使用“union”,sql会对结果进行区分,从而影响查询的性能
50few1ms2#
很容易:)这应该适用于最常见的db引擎:
正如edamame所说的,你可能需要阅读每个团队的两个结果。这个问题本身并不清楚,但可以这样解决: