2
我在鏈接數據庫中有兩個查詢(鏈接來自兩個不同項目數據庫的表)。我需要組合查詢來獲得按Interaction_Type1分組的總數。 SQL代碼如下:總結兩個SELECT COUNT與GROUP BY查詢
查詢#1:
SELECT Sum(Temp.cnt) AS SumOfcnt, Temp.Interaction_Type1
FROM (SELECT COUNT(*) as cnt, Interaction_Type1 from AMERILOCKMasterConversionTable
GROUP BY Interaction_Type1
UNION ALL
SELECT COUNT(*), Interaction_Type2 from AMERILOCKMasterConversionTable
GROUP BY Interaction_Type2
UNION ALL
SELECT COUNT(*), Interaction_Type3 from AMERILOCKMasterConversionTable
GROUP BY Interaction_Type3
) AS Temp
GROUP BY Temp.Interaction_Type1;
和查詢#2
SELECT Sum(Temp.cnt) AS SumOfcnt, Temp.Interaction_Type1
FROM (SELECT COUNT(*) as cnt, Interaction_Type1 from MARKETMasterConversionTable
GROUP BY Interaction_Type1
UNION ALL
SELECT COUNT(*), Interaction_Type2 from MARKETMasterConversionTable
GROUP BY Interaction_Type2
UNION ALL
SELECT COUNT(*), Interaction_Type3 from MARKETMasterConversionTable
GROUP BY Interaction_Type3
) AS Temp
GROUP BY Temp.Interaction_Type1;
我想查詢一代產量如下結果:
Interaction_Type1 Total
Left_Message 23
Made_Contact 16
Bad_Phone_Number 8
No_Answer 12
我是SQL的新手,並且已經在線研究了所有這些內容,並且沒有將這兩個查詢結合起來生成預期的結果。
任何幫助將非常感謝!
謝謝! 紅
我不是SQL大師;可能會有更好的答案,但如果我提出了這個問題,我會將所有數據插入臨時表中,然後從那裏做最後的SELECT。 – Melanie