Exercise: Grouping events by year and month

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

The answer to the exercise will be included and explained if you attend the Wise Owl course listed below!

Category ==> SQL  (115 exercises)
Topic ==> More complicated grouping  (1 exercise)
Level ==> Harder than average
Course ==> Introduction to SQL
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

If you haven't already got it set up in SQL server, restore the Historical Events database from the above folder.

Create a query to list the events by year and month, with the most recent event coming first.  You may find the syntax of the DatePart function useful:

SQL exercise - More complicated grouping (image 1)

This would show the event year as a 4-digit number

Your final output should look like this:

SQL exercise - More complicated grouping (image 2)

Don't worry about the missing eventless months

If you get this working, try adding WITH CUBE or WITH ROLLUP immediately after your GROUP BY clause to see the effect of this.   You should find that the ROLLUP option gives you yearly subtotals, while the CUBE option also gives you totals by month at the end.

Save your query as Events by date, then close it down.

This page has 0 threads Add post