简体   繁体   English

在SQL Server查询的底部添加总计行

[英]Adding a total row at the bottom in a SQL Server query

I have two tables in SQL Server 2008 created like: 我在SQL Server 2008中创建了两个表,如下所示:

CompletedRecordsFinal : 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

Example result: 结果示例:

C1  C2  C3  TotalRecords    
A   B   1       50          
A   B   2       100 

WaitRecordsFinal : 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

Example result: 结果示例:

C1  C2  C3  TotalWaitRecords    
A   B   1       20  
A   B   2       30  

I use these tables with a left outer join like: 我将这些表与左外部联接一起使用,例如:

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 

What I want is to add a total row at the bottom and I couldn't manage it. 我想要在底部添加一个总行,但我无法管理它。 I feel I am close but nothing worked like I wanted up to now. 我觉得自己很亲近,但是到目前为止,没有任何事情像我想要的那样工作。 I need something like (it doesn't work): 我需要类似的东西(不起作用):

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 

Example output of what I want: 我想要的示例输出:

   C1   C2   C3  TotalRecords   TotalWaitRecords    
    A   B     1     50              20  
    A   B     2     100             30
Total Total  -1     150             50   

Any help would be appreciated. 任何帮助,将不胜感激。

EDIT: My problem is a little different than the one indicated as duplicate subject. 编辑:我的问题与指示为重复主题的问题有些不同。 The main problem is I have 3 column for group by. 主要问题是我有3列用于分组依据。 therefore when I do group by C1, C2, C3, with rollup I see that: 因此,当我group by C1, C2, C3, with rollup进行group by C1, C2, C3, with rollupgroup 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

I just need the most bottom row, not the others. 我只需要最底层的行,而不需要其他行。 So this solution does not help. 因此,此解决方案无济于事。 How can I get rid of the others except the most bottom Total Total one? 我如何摆脱除最底端的总数之外的其他总数?

You can use WITH ROLLUP and also use GROUPING() to label the totals. 您可以使用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

you can try doing it with a UNION ALL . 您可以尝试使用UNION ALL I tried using a CTE to combine your 2 queries into one.. 我尝试使用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

SQL Fiddle Demo SQL小提琴演示

If i just use the Temp tables you are creating already, you should be able to simply do this. 如果我只是使用已经创建的临时表,则应该可以轻松完成此操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM