2016-08-16 9 views
0

に(別の句を使用して)複数のクエリを組み合わせた:SQL /インパラ:私は次のクエリ持っている1

'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つにこれらの2つのクエリを結合することは可能ですか?ありがとう!

答えて

1

簡単に:)これは、最も一般的なDBエンジンで動作するはずです:

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 

枝豆は、あなたがチームごとの結果の両方を読むことをお勧めします、述べたように。それは質問自体から明らかではなかったが、この方法で解決することができる:uは返された結果が異なっていると思われる場合uが唯一の「UNION」で動作するので

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 
+0

結果はチームを使用して参加しません。行の半分にはdistinct_id_count_w2 = Noneがあり、残りの半分にはdistinct_id_count_w1 = Noneがあります。各行は、distinct_id_count_w1の有効な値とdistinct_id_count_w2の有効な値を持つ必要があります – Edamame

0

、uは「UNION ALL」を使用する必要があり、SQLをクエリーの結果に影響する結果を区別します

関連する問題