我在SQL Server 2014中有这个简单的联合查询,我从每个表中获取行数,然后尝试在底部添加一个TOTAL行,它将对两个表中的计数进行SUM计算。我认为问题是最后一个联盟的LEFT OUTER JOIN似乎只是从第一个表中总计的总和
SELECT A.TEST_CODE, B.DIVISION, COUNT(*)
FROM ALL_USERS B, SIGMA_TEST A
WHERE B.DOMID = A.DOMID
GROUP BY A.TEST_CODE, B.DIVISION
UNION
SELECT E.TEST_CODE, F.DIVISION, COUNT(*)
FROM BETA_TEST E, ALL_USERS F
WHERE E.DOMID = F.DOMID
GROUP BY E.TEST_CODE, F.DIVISION
UNION
SELECT 'TOTAL', '', COUNT(*)
FROM (SIGMA_TEST A LEFT OUTER JOIN BETA_TEST E ON A.DOMID
= E.DOMID )
以下是我得到的结果示例:
我希望TOTAL行显示 6 (2 + 1 + 3 = 6)的结果
如果可能,我想避免使用公用表表达式(CTE)。提前谢谢!
答案 0 :(得分:3)
由于您在前两个语句中计算具有匹配DOMID的用户,因此最终语句还需要包含ALL_USERS表。最后的陈述应该是:
SELECT 'TOTAL', '', COUNT(*)
FROM ALL_USERS G LEFT OUTER JOIN
SIGMA_TEST H ON G.DOMID = H.DOMID
LEFT OUTER JOIN BETA_TEST I ON I.DOMID = G.DOMID
WHERE (H.TEST_CODE IS NOT NULL OR I.TEST_CODE IS NOT NULL)
答案 1 :(得分:1)
我认为您可以通过单个查询实现此目的。看来您的测试表具有相似的结构,因此您可以将它们组合在一起并加入ALL_USERS
,最后,您可以使用GROUPING SETS
来获取总数
SELECT ISNULL(T.TEST_CODE, 'TOTAL') AS TEST_CODE,
ISNULL(U.DIVISION, '') AS DIVISION,
COUNT(*)
FROM ALL_USERS AS U
INNER JOIN
( SELECT DOMID, TEST_CODE, 'SIGNMA' AS SOURCETABLE
FROM SIGMA_TEST
UNION ALL
SELECT DOMID, TEST_CODE, 'BETA' AS SOURCETABLE
FROM BETA_TEST
) AS T
ON T.DOMID = U.DOMID
GROUP BY GROUPING SETS ((T.TEST_CODE, U.DIVISION, T.SOURCETABLE), ());
另外,the implicit join syntax you are using was replaced over a quarter of a century ago in ANSI 92。这没有错,但似乎没有理由继续使用它,特别是当你混合和匹配显式外连接和隐式内连接时。任何可能阅读SQL的人肯定会欣赏一致性。
答案 2 :(得分:1)
我会考虑首先UNION ALL
然后 COUNT
:
SELECT COALESCE(TEST_CODE, 'TOTAL'),
DIVISION,
COUNT(*)
FROM (
SELECT A.TEST_CODE, B.DIVISION
FROM ALL_USERS B
INNER JOIN SIGMA_TEST A ON B.DOMID = A.DOMID
UNION ALL
SELECT E.TEST_CODE, F.DIVISION
FROM BETA_TEST E
INNER JOIN ALL_USERS F ON E.DOMID = F.DOMID ) AS T
GROUP BY GROUPING SETS ((TEST_CODE, DIVISION ), ())
使用GROUPING SETS
可以轻松获得总数,因此无需添加第三个子查询。
注意:我假设您希望每(TEST_CODE, DIVISION)
只有一个计数。否则你也必须在源表上进行分组,就像@Gareth的回答一样。