562 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Software ==> | SQL (129 exercises) |
Topic ==> | Aggregation and grouping (8 exercises) |
Level ==> | Harder than average |
Subject ==> | SQL training |
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.
This will generate the database that you'll need to use in order to do this exercise (note that the database and script are only to be used for exercises published on this website, and may not be reused or distributed in any form without the prior written permission of Wise Owl).
Create a query to show the following information:
You'll need to calculate the century for each event date, and group by this.
To get the top row giving the grand total in the same query, encase your GROUP BY expression in the CUBE function.
Save this query as Passing of the centuries, then close it down.
You can find other training resources for the subject of this exercise here:
From: | Rupchand7 |
When: | 14 Nov 23 at 07:18 |
select
case when datepart(year,eventdate) like '17%' then '18th century'
when datepart(year,eventdate) like '18%' then '19th century'
when datepart(year,eventdate) like '19%' then '20th century'
else '21th century'
end as century, count(eventid) as [Number of Events] from tblEvent group by
cube( case when datepart(year,eventdate) like '17%' then '18th century'
when datepart(year,eventdate) like '18%' then '19th century'
when datepart(year,eventdate) like '19%' then '20th century'
else '21th century'
end) order by century
From: | 4mj4d |
When: | 13 Aug 23 at 13:35 |
select
CAST(CAST(left(EventDate, 2) as int) + 1 as varchar)+ 'th century' as [Number of events],
COUNT(left(EventDate, 2))
from tblEvent
GROUP BY CUBE(left(EventDate, 2))
ORDER BY [Number of events]
From: | mvh_analyst |
When: | 15 Dec 22 at 09:20 |
Didn't see this as a solution yet:
select concat((year(EventDate) / 100) + 1,'th century') as century,
count(*) as [Number of events]
from tblEvent
group by concat((year(EventDate) / 100) + 1,'th century')
From: | linjom |
When: | 23 Apr 18 at 07:33 |
I am getting different result..
NULL 459
18th Century 4
19th Century 14
20th Century 396
21st Century 45
for both rollup and cube.. is this right? Final total same as site.
From: | Zerish |
When: | 22 May 18 at 05:43 |
This will give you exact same result as posted on site
USE WorldEvents
GO
SELECT
CASE
WHEN DATEPART(YY,tblEvent.EventDate) like '18%' THEN '19th Century'
WHEN DATEPART(YY,tblEvent.EventDate) like '17%' THEN '18th Century'
WHEN DATEPART(YY,tblEvent.EventDate) like '19%' THEN '20th Century'
WHEN DATEPART(YY,tblEvent.EventDate) like '20%' THEN '21st Century'
END AS Century,COUNT(tblEvent.EventName) AS NumberOfEvents
FROM tblEvent
GROUP BY CUBE(CASE
WHEN DATEPART(YY,tblEvent.EventDate) like '18%' THEN '19th Century'
WHEN DATEPART(YY,tblEvent.EventDate) like '17%' THEN '18th Century'
WHEN DATEPART(YY,tblEvent.EventDate) like '19%' THEN '20th Century'
WHEN DATEPART(YY,tblEvent.EventDate) like '20%' THEN '21st Century'
END)
ORDER BY NumberOfEvents DESC
From: | ShukiMolk |
When: | 12 Apr 20 at 16:55 |
This is way too specific. What will happen once more event are updated to the database? And there might be events from before the 18th century.
I believe we should write a code that can be used over more data.
How about this:
SELECT
(CAST(((YEAR(EventDate)/100)+1) AS varchar) + 'th century') AS 'Century',
COUNT(CAST(((YEAR(EventDate)/100)+1) AS varchar) + 'th century') AS 'Number of events'
FROM
[tblEvent]
GROUP BY
(CAST(((YEAR(EventDate)/100)+1) AS varchar) + 'th century')
WITH CUBE
From: | SeahawkZim |
When: | 22 Mar 20 at 00:09 |
Hi, I would like to offer these observations by using the code below:
use worldevents
go
SELECT CASE LEFT(CONVERT(VARCHAR(10), eventdate, 121), 2) WHEN '20' THEN '21st century'
WHEN '19' THEN '20th century'
WHEN '18' THEN '19th century'
WHEN '17' THEN '18th century' end as Century,
COUNT(eventid) AS [Number of Events]
FROM tblevent
GROUP BY CUBE (CASE LEFT(CONVERT(VARCHAR(10), eventdate, 121), 2) WHEN '20' THEN '21st century'
WHEN '19' THEN '20th century'
WHEN '18' THEN '19th century'
WHEN '17' THEN '18th century' end)
ORDER BY Century
Using a simple CASE construct, makes the code easier to read. Grouping on the eventid (primary key) ensures an accurate count. Grouping on the eventname (nullable) doesn't ensure you will get an accurate count. Also, using an explicit conversion avoids the sql engine from having to do an implicit conversion from date to string so that the LIKE statement can be used. Also, LIKE and wildcards are usually a drag on performance but in this case with only a few rows, the hit is not noticeable. In the actual performance plan, the buffer size using this code is 1/4 the size of the code using LIKE. Going up against a lot of data could produce a memory hit.
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.