ComeçarComece de graça

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.

Este exercício faz parte do curso

Time Series Analysis in SQL Server

Ver curso

Instruções do exercício

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

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

-- 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';
Editar e executar o código