IniziaInizia gratis

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.

Questo esercizio fa parte del corso

Time Series Analysis in SQL Server

Visualizza il corso

Istruzioni dell'esercizio

  • Return the count of distinct values in IncidentTypeID as NumberOfIncidentTypes.
  • Return the count of distinct values in IncidentDate as NumberOfDaysWithIncidents

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

-- 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';
Modifica ed esegui il codice