Generate a summary with ROLLUP
The ROLLUP operator works best when your non-measure attributes are hierarchical. Otherwise, you may end up weird aggregation levels which don't make intuitive sense.
In this scenario, we wish to aggregate the total number of security incidents in the IncidentRollup table. Management would like to see data aggregated by the combination of calendar year, calendar quarter, and calendar month. In addition, they would also like to see separate aggregate lines for calendar year plus calendar quarter, as well as separate aggregate lines for each calendar year. Finally, they would like one more line for the grand total. We can do all of this in one operation.
Este ejercicio forma parte del curso
Time Series Analysis in SQL Server
Instrucciones del ejercicio
- Complete the definition for
NumberOfIncidentsby adding up the number of incidents over each range. - Fill out the
GROUP BYsegment, including theWITH ROLLUPoperator.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
SELECT
c.CalendarYear,
c.CalendarQuarterName,
c.CalendarMonth,
-- Include the sum of incidents by day over each range
___(ir.___) AS NumberOfIncidents
FROM dbo.IncidentRollup ir
INNER JOIN dbo.Calendar c
ON ir.IncidentDate = c.Date
WHERE
ir.IncidentTypeID = 2
GROUP BY
-- GROUP BY needs to include all non-aggregated columns
c.___,
c.___,
c.___
-- Fill in your grouping operator
WITH ___
ORDER BY
c.CalendarYear,
c.CalendarQuarterName,
c.CalendarMonth;