使用統計彙總函式
SQL Server 提供多個用於統計用途的彙總函式。AVG() 會計算樣本的平均值。STDEV() 與 STDEVP() 分別給出樣本與母體的標準差。VAR() 與 VARP() 分別給出樣本與母體的變異數。除此之外,還有你在上一個練習中學到的彙總函式,例如 SUM()、COUNT()、MIN() 和 MAX()。
在這個練習中,我們會再次查看事件彙總與事件類型資料,這次聚焦在西元 2020 年的第 2 季。我們想了解事件發生的離散程度——也就是說,觀察每天的事件數是否穩定,或是波動較大。
本練習屬於課程
SQL Server 的時間序列分析
練習說明
- 填入缺少的彙總函式。標準差與變異數請使用樣本函式,而非母體函式。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
-- Fill in the missing function names
SELECT
it.IncidentType,
___(ir.NumberOfIncidents) AS MeanNumberOfIncidents,
___(CAST(ir.NumberOfIncidents AS DECIMAL(4,2))) AS MeanNumberOfIncidents,
___(ir.NumberOfIncidents) AS NumberOfIncidentsStandardDeviation,
___(ir.NumberOfIncidents) AS NumberOfIncidentsVariance,
___(1) AS NumberOfRows
FROM dbo.IncidentRollup ir
INNER JOIN dbo.IncidentType it
ON ir.IncidentTypeID = it.IncidentTypeID
INNER JOIN dbo.Calendar c
ON ir.IncidentDate = c.Date
WHERE
c.CalendarQuarter = 2
AND c.CalendarYear = 2020
GROUP BY
it.IncidentType;