使用 CUBE 檢視所有彙總
CUBE 運算子會對所有組合進行交叉彙總,可能產生非常多的列。這個運算子最適合用在非階層式資料上,當你同時關心各欄位獨立的彙總與其組合的彙總時特別有用。
在這個情境中,我們想要找出 IncidentRollup 資料表中的安全事件總數,但不會依照正規的階層來分組。相反地,我們會著重在彙總數個彼此無關的屬性。
本練習屬於課程
SQL Server 的時間序列分析
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
SELECT
-- Use the ORDER BY clause as a guide for these columns
-- Don't forget that comma after the third column if you
-- copy from the ORDER BY clause!
ir.___,
c.___,
c.___,
SUM(ir.NumberOfIncidents) AS NumberOfIncidents
FROM dbo.IncidentRollup ir
INNER JOIN dbo.Calendar c
ON ir.IncidentDate = c.Date
WHERE
ir.IncidentTypeID IN (3, 4)
GROUP BY
-- GROUP BY should include all non-aggregated columns
ir.___,
c.___,
c.___
-- Fill in your grouping operator
WITH ___
ORDER BY
ir.IncidentTypeID,
c.CalendarQuarterName,
c.WeekOfMonth;