我有以下查询。
是否可以在最后一行汇总所有列?即总计,星期一,星期二,星期三,星期四,星期五,星期六,星期日列。
SELECT Format(onhour, '00') + ':00' AS Hour,
Sum(totals) AS Total,
Avg(CASE WHEN DayOfWeek = 'Monday' THEN totals END) AS Monday ,
Avg(CASE WHEN dayofweek = 'Tuesday' THEN totals END) AS Tuesday ,
Avg(CASE WHEN dayofweek = 'Wednesday' THEN totals END) AS Wednesday,
Avg(CASE WHEN dayofweek = 'Thursday' THEN totals END) AS Thursday,
Avg(CASE WHEN dayofweek = 'Friday' THEN totals END) AS Friday ,
Avg(CASE WHEN dayofweek = 'Saturday' THEN totals END) AS Saturday,
Avg(CASE WHEN dayofweek = 'Sunday' THEN totals END) AS Sunday
FROM (SELECT Datename(weekday, D.fordate) AS DayOfWeek,
H.onhour,
Isnull(T.totals, 0) AS Totals
FROM (SELECT DISTINCT T.fordate
FROM #visit AS T) AS D
CROSS apply (VALUES(0),
(1),
(2),
(3),
(4),
(5),
(6),
(7),
(8),
(9),
(10),
(11),
(12),
(13),
(14),
(15),
(16),
(17),
(18),
(19),
(20),
(21),
(22),
(23)) AS H(onhour)
LEFT OUTER JOIN #visit AS T
ON H.onhour = T.onhour
AND D.fordate = T.fordate) a
WHERE onhour NOT IN ( 0, 1, 2, 3,
4, 5, 6, 7,
20, 21, 22, 23 )
GROUP BY onhour
ORDER BY onhour;
答案 0 :(得分:0)
是的。使用grouping sets
:
GROUP BY GROUPING SETS ( (onhour), () )
您可能希望将ORDER BY
更改为:
ORDER BY GROUPING(onhour), onhour