我在SQL Server 2008中创建了两个表,如:
CompletedRecordsFinal
:
IF OBJECT_Id('tempdb..#CompletedRecordsFinal') IS NOT NULL
DROP TABLE #CompletedRecordsFinal
SELECT
C1, C2, C3, COUNT(distinct Id) as TotalRecords
INTO
#CompletedRecordsFinal
FROM
#CompletedRecords
GROUP BY
C1, C2, C3
示例结果:
C1 C2 C3 TotalRecords
A B 1 50
A B 2 100
WaitRecordsFinal
:
IF OBJECT_Id('tempdb..#WaitRecordsFinal') IS NOT NULL
DROP TABLE #WaitRecordsFinal
SELECT
C1, C2, C3, COUNT(distinct Id) as TotalWaitRecords
INTO
#WaitRecordsFinal
FROM
#WaitRecords
GROUP BY
C1, C2, C3
示例结果:
C1 C2 C3 TotalWaitRecords
A B 1 20
A B 2 30
我将这些表与左外连接一起使用,如:
SELECT
w.C1, C.C2, w.C3, TotalWaitRecords, TotalRecords
FROM
#WaitingRecordsFinal w
LEFT OUTER JOIN
#CompletedRecordsFinal c ON c.C1 = w.C1
AND c.C2 = w.C2
AND c.C3 = w.C3
我想要的是在底部添加一行,我无法管理它。我觉得我很亲密,但没有像我现在想的那样工作。我需要类似的东西(它不起作用):
SELECT
w.C1, C.C2, w.C3, TotalWaitRecords, TotalRecords
FROM
#WaitingRecordsFinal w
LEFT OUTER JOIN
#CompletedRecordsFinal c ON c.C1 = w.C1
AND c.C2 = w.C2
AND c.C3 = w.C3
UNION
SELECT
C1 = 'Total', C2 = 'Total', C3 = -1, TotalWaitRecords, TotalRecords
FROM
#WaitingRecordsFinal w
LEFT OUTER JOIN
#CompletedRecordsFinal c ON c.C1 = w.C1
AND c.C2 = w.C2
AND c.C3 = w.C3
我想要的输出示例:
C1 C2 C3 TotalRecords TotalWaitRecords
A B 1 50 20
A B 2 100 30
Total Total -1 150 50
任何帮助都将不胜感激。
编辑:我的问题与指示为重复主题的问题略有不同。主要问题是我有3列用于分组。因此,当我group by C1, C2, C3, with rollup
时,我看到了:
.....
X B -1 300 500
X Total -1 300 500
.....
A B -1 56 47
A Total -1 56 47
Total Total -1 356 547
我只需要最底线,而不是其他。所以这个解决方案没有帮助。我怎样才能摆脱除最底层总数之外的其他人?
答案 0 :(得分:2)
您可以使用WITH ROLLUP并使用GROUPING()标记总计。
Select CASE WHEN GROUPING(w.C1) = 1 THEN 'Total' ELSE w.c1
, CASE WHEN GROUPING(C.C2) = 1 THEN 'Total' ELSE c.c2
, CASE WHEN GROUPING(w.C3) = 1 THEN 'Total' ELSE w.c3
, SUM(TotalWaitRecords) AS TotalWaitRecords
, SUM(TotalRecords) AS TotalRecords
from #WaitingRecordsFinal w
left outer join #CompletedRecordsFinal c
on c.C1 = w.C1
and c.C2 = w.C2
and c.C3 = w.C3
GROUP BY
w.C1
, C.C2
, w.C3
with rollup
答案 1 :(得分:1)
您可以尝试使用UNION ALL
。我尝试使用CTE将您的2个查询合并为一个..
WITH cte AS
(
SELECT wrf.C1,
wrf.C2,
wrf.C3,
crf.TotalRecords,
SUM(wrf.TotalWaitRecords) AS TotalWaitRecords
FROM (SELECT C1,
C2,
C3,
COUNT(DISTINCT Id) AS TotalWaitRecords
FROM #WaitRecords
GROUP BY C1,
C2,
C3
) wrf
OUTER APPLY (SELECT crf.C1,
crf.C2,
crf.C3,
SUM(TotalRecords) AS TotalRecords
FROM (SELECT C1,
C2,
C3,
COUNT(DISTINCT Id) AS TotalRecords
FROM #CompletedRecords
GROUP BY C1,
C2,
C3
) crf
WHERE crf.C1 = wrf.C1
AND crf.C2 = wrf.C2
AND crf.C3 = wrf.C3
GROUP BY crf.C1,
crf.C2,
crf.C3
) crf
GROUP BY wrf.C1,
wrf.C2,
wrf.C3,
crf.TotalRecords
)
SELECT *
FROM cte
UNION ALL
SELECT 'Total',
'Total',
-1,
SUM(TotalRecords) TotalRecords,
SUM(TotalWaitRecords) TotalWaitRecords
FROM cte
如果我只使用你正在创建的临时表,你应该可以简单地这样做。
IF OBJECT_Id('tempdb..#AllRecordsFinal') IS NOT NULL
DROP TABLE #AllRecordsFinal
SELECT wrf.C1,wrf.C2,wrf.C3,crf.TotalRecords,wrf.TotalWaitRecords
INTO #AllRecordsFinal
FROM #WaitRecordsFinal wrf
OUTER APPLY (
SELECT TotalRecords
FROM #CompletedRecordsFinal cr
WHERE cr.C1 = wrf.C1 AND cr.C2 = wrf.C2 AND cr.C3 = wrf.C3) crf
SELECT * FROM #AllRecordsFinal
UNION ALL
SELECT 'Total','Total',-1,
SUM(TotalRecords),
SUM(TotalWaitRecords)
FROM #AllRecordsFinal