2012-08-28 70 views
1

我試圖在同一個表上總結以下三個查詢的結果以生成一個單一報表。從每個查詢如何總結同一表的三個不同查詢的結果

SELECT b.BG_SEVERITY as Severity, count (b.BG_BUG_ID) as Total 
FROM BUG b where 
b.BG_USER_12='QA3' 
group by b.BG_SEVERITY 

SELECT s.BG_SEVERITY as Severity, count (s.BG_BUG_ID) as Total 
FROM BUG s where 
s.BG_USER_12='QA1' 
group by s.BG_SEVERITY 

SELECT a.BG_SEVERITY as Severity, count (a.BG_BUG_ID) as Total 
FROM BUG a where 
a.BG_USER_12='QA2' 
group by a.BG_SEVERITY 

結果將類似於下面:

Severity Total 
1-Critical 4 
2-High  94 
3-Medium 23 
4-Low  45 

我期待從上述三個查詢組合/總結中的「合計」一欄。我可以用什麼SQL功能來做到這一點?

+1

如果其中一個答案解決了您的問題,請將其標記爲已接受。 :) –

回答

1

如果使用sql-server,你可以嘗試使用union。這樣的事情:

SELECT 
    Severity, 
    SUM(Total) 
FROM 
    (
    SELECT b.BG_SEVERITY as Severity, count (b.BG_BUG_ID) as Total FROM BUG b where b.BG_USER_12='QA3' group by b.BG_SEVERITY 
    UNION ALL 
    SELECT s.BG_SEVERITY as Severity, count (s.BG_BUG_ID) as Total FROM BUG s where s.BG_USER_12='QA1' group by s.BG_SEVERITY  
    UNION ALL 
    SELECT a.BG_SEVERITY as Severity, count (a.BG_BUG_ID) as Total FROM BUG a where a.BG_USER_12='QA2' group by a.BG_SEVERITY 
    ) tbl 
GROUP BY 
    Severity 
+0

謝謝Much..it工作 – vnai

1

您可以使用IN在您的情況。

SELECT b.BG_SEVERITY as Severity, 
     COUNT(b.BG_BUG_ID) as Total 
FROM  BUG b 
WHERE b.BG_USER_12 IN ('QA3','QA1','QA2') 
GROUP BY b.BG_SEVERITY 
+0

很好的答案!謝謝。 –

相關問題