我正在尝试将同一个表上的三个查询的结果相加以生成一个单独的报告。
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的哪些功能来执行此操作?
答案 0 :(得分: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
答案 1 :(得分: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