2013-12-23 66 views
2

我目前有一個查詢可以彙總一組值並按日期對它們進行分組。其結果將是這個樣子:在彙總SUM列的查詢末尾插入行

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) 

我想看到的是一個「總計」插入行在結果的最後。最終輸出看起來像

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

回答

4

聯合查詢將是一個快速和骯髒的方式做到這一點

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 
+1

這是一些快速的寫作,埃利亞斯! – jp2code

+1

我很熟悉複製pastaaaa。 – Elias

+1

沒有開玩笑,非常令人印象深刻的埃利亞斯。謝謝= D –

5

您還可以使用WITH ROLLUP在GROUP選項,通過條款

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)