Calculating distinct counts
The COUNT()
function has a variant which can be quite useful: COUNT(DISTINCT)
. This distinct count function allows us to calculate the number of unique elements in a data set, so COUNT(DISTINCT x.Y)
will get the unique number of values for column Y
on the table aliased as x
.
In this example, you will continue to look at incident rollup data in the dbo.IncidentRollup
table. Management would like to know how many unique incident types we have in our three-month data set as well as the number of days with incidents.
They already know the total number of incidents because you gave them that information in the last exercise.
Cet exercice fait partie du cours
Time Series Analysis in SQL Server
Instructions
- Return the count of distinct values in
IncidentTypeID
asNumberOfIncidentTypes
. - Return the count of distinct values in
IncidentDate
asNumberOfDaysWithIncidents
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
-- Fill in the functions and columns
SELECT
___(___ ir.___) AS NumberOfIncidentTypes,
___(___ ir.___) AS NumberOfDaysWithIncidents
FROM dbo.IncidentRollup ir
WHERE
ir.IncidentDate BETWEEN '2019-08-01' AND '2019-10-31';