SQL | Archived 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.

Software ==> SQL  (198 exercises)
Version ==> Any version of SQL Server
Topic ==> Archived  (70 exercises)
Level ==> Harder than average
Subject ==> SQL training
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.

You can unzip this file to see the answers to this exercise, although please remember this is for your personal use only.
This page has 0 threads Add post