使用Totals for SUM列在​​查询末尾插入ROW

时间:2013-12-23 20:11:20

标签: sql sql-server sql-server-2008 tsql

我目前有一个查询,它总结了一组值并按日期对它们进行分组。结果看起来像这样:

Date        Item2   Item2   Item3   Item4   Item5
2013-05-31  1       30      0       0       129
2013-06-03  0       90      0       15      78
2013-06-04  0       50      0       1       124
2013-06-05  0       100     0       17      58
2013-06-06  0       24      0       0       105
2013-06-07  0       50      0       1       83
2013-06-10  0       45      2       42      64
2013-06-11  3       43      0       14      90
2013-06-12  2       44      0       36      88
2013-06-13  1       34      0       15      92

我用来检索该结果的查询看起来像这样

SELECT CONVERT(NVARCHAR(10), TheDate, 120) 'Date',  
            (SUM(CASE WHEN itemID = 1 THEN 1 ELSE 0 END)) AS Item1,
            (SUM(CASE WHEN itemID = 2 THEN 1 ELSE 0 END)) AS Item2,
            (SUM(CASE WHEN itemID = 3 THEN 1 ELSE 0 END)) AS Item3,
            (SUM(CASE WHEN itemID = 4 THEN 1 ELSE 0 END)) AS Item4,
            (SUM(CASE WHEN itemID = 5 THEN 1 ELSE 0 END)) AS Item5
FROM         Table1 AS a INNER JOIN
                      Table2 AS tab2 ON tab2.Table1ID = a.Table1ID LEFT OUTER JOIN
                      Table3 AS tab3 ON tab3.Table2ID = tab2.Table2ID
WHERE someOtherID = 3 AND UserID = 40
GROUP BY CONVERT(NVARCHAR(10), TheDate, 120)
ORDER BY CONVERT(NVARCHAR(10), TheDate, 120)

我希望看到的是在结果末尾插入的“GRAND TOTAL”行。最终输出看起来像

Date        Item2   Item2   Item3   Item4   Item5
2013-05-31  1       30      0       0       129
2013-06-03  0       90      0       15      78
2013-06-04  0       50      0       1       124
2013-06-05  0       100     0       17      58
2013-06-06  0       24      0       0       105
2013-06-07  0       50      0       1       83
2013-06-10  0       45      2       42      64
2013-06-11  3       43      0       14      90
2013-06-12  2       44      0       36      88
2013-06-13  1       34      0       15      92
GRAND TOTAL 7       510     2       141     911

有没有人对最佳方法有任何建议?

谢谢= D

2 个答案:

答案 0 :(得分:7)

您还可以在 GROUP BY 子句中使用WITH ROLLUP 选项

SELECT CASE GROUPING([TheDate])
              WHEN 1 THEN 'GRAND TOTAL'
              ELSE CONVERT(NVARCHAR(10), TheDate, 120) 
              END as Date
   ...
FROM         Table1 AS a INNER JOIN
                  Table2 AS tab2 ON tab2.Table1ID = a.Table1ID LEFT OUTER JOIN
                  Table3 AS tab3 ON tab3.Table2ID = tab2.Table2ID
WHERE someOtherID = 3 AND UserID = 40
GROUP BY CONVERT(NVARCHAR(10), TheDate, 120) WITH ROLLUP
ORDER BY CONVERT(NVARCHAR(10), TheDate, 120)

答案 1 :(得分:5)

联合查询将是一种快速而又脏的方法

SELECT CONVERT(NVARCHAR(10), TheDate, 120) 'Date',  
        (SUM(CASE WHEN itemID = 1 THEN 1 ELSE 0 END)) AS Item1,
        (SUM(CASE WHEN itemID = 2 THEN 1 ELSE 0 END)) AS Item2,
        (SUM(CASE WHEN itemID = 3 THEN 1 ELSE 0 END)) AS Item3,
        (SUM(CASE WHEN itemID = 4 THEN 1 ELSE 0 END)) AS Item4,
        (SUM(CASE WHEN itemID = 5 THEN 1 ELSE 0 END)) AS Item5
FROM         Table1 AS a INNER JOIN
                  Table2 AS tab2 ON tab2.Table1ID = a.Table1ID LEFT OUTER JOIN
                  Table3 AS tab3 ON tab3.Table2ID = tab2.Table2ID
WHERE someOtherID = 3 AND UserID = 40
GROUP BY CONVERT(NVARCHAR(10), TheDate, 120)
ORDER BY CONVERT(NVARCHAR(10), TheDate, 120)

UNION 

    SELECT 'GrandTotal',  
        (SUM(CASE WHEN itemID = 1 THEN 1 ELSE 0 END)) AS Item1,
        (SUM(CASE WHEN itemID = 2 THEN 1 ELSE 0 END)) AS Item2,
        (SUM(CASE WHEN itemID = 3 THEN 1 ELSE 0 END)) AS Item3,
        (SUM(CASE WHEN itemID = 4 THEN 1 ELSE 0 END)) AS Item4,
        (SUM(CASE WHEN itemID = 5 THEN 1 ELSE 0 END)) AS Item5
FROM         Table1 AS a INNER JOIN
                  Table2 AS tab2 ON tab2.Table1ID = a.Table1ID LEFT OUTER JOIN
                  Table3 AS tab3 ON tab3.Table2ID = tab2.Table2ID
WHERE someOtherID = 3 AND UserID = 40