Good job! We can also create a more advanced version of this report by grouping all rows using multiple date parts. For instance, suppose we need to find the registration count for each quarter of each year. This will allow us to compare values for the same quarter across different years.
DATEPART(year, RegistrationDate) AS RegistrationYear,
DATEPART(quarter, RegistrationDate) AS RegistrationQuarter,
COUNT(CustomerID) AS RegistrationCount
GROUP BY DATEPART(year, RegistrationDate), DATEPART(quarter, RegistrationDate)
ORDER BY DATEPART(year, RegistrationDate), DATEPART(quarter, RegistrationDate);
As you can see, we now use the
DATEPART() function twice: the first time with the
RegistrationYear column and the second time with the
RegistrationQuarter column. Note that we use both columns in three places: in the
GROUP BY, and
ORDER BY clauses.