Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers 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 (203 exercises) |
Topic ==> | Aggregation and grouping (12 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: | Bhavya59 |
When: | 01 Aug 24 at 13:48 |
SELECT CASE
WHEN EventDate>='1701-01-01' AND EventDate<'1801-01-01' THEN '18th Century'
WHEN EventDate>='1801-01-01' AND EventDate<'1901-01-01' THEN '19th Century'
WHEN EventDate>='1901-01-01' AND EventDate<'2001-01-01' THEN '20th Century'
WHEN EventDate>='2001-01-01' THEN '21th Century'
ELSE ''
END AS Century
,COUNT(EventID)
FROM tblEvent
GROUP BY CUBE((CASE
WHEN EventDate>='1701-01-01' AND EventDate<'1801-01-01' THEN '18th Century'
WHEN EventDate>='1801-01-01' AND EventDate<'1901-01-01' THEN '19th Century'
WHEN EventDate>='1901-01-01' AND EventDate<'2001-01-01' THEN '20th Century'
WHEN EventDate>='2001-01-01' THEN '21th Century'
ELSE ''
END))
From: | roqovzki |
When: | 12 Feb 24 at 23:13 |
So, this is my pretty simple or maybe even dumb query (as I'm a beginner), but I came across this exercise and it seems to me that the solution given in the files and most of the users replies are incorrect.
I belivee most of the solutions miss out on one year of each century, as the century lasts from XX01-01-01 till XX00-12-31.
Let me know guys whether I'm completely missing on something.
SELECT
COUNT(*)
,CASE
WHEN EventDate BETWEEN '1701-01-01' AND '1800-12-31' THEN '18th century'
WHEN EventDate BETWEEN '1801-01-01' AND '1900-12-31' THEN '19th century'
WHEN EventDate BETWEEN '1901-01-01' AND '2000-12-31' THEN '20th century'
WHEN EventDate BETWEEN '2001-01-01' AND '2100-12-31' THEN '21th century'
END AS [Century]
FROM
tblEvent
GROUP BY CUBE
(CASE
WHEN EventDate BETWEEN '1701-01-01' AND '1800-12-31' THEN '18th century'
WHEN EventDate BETWEEN '1801-01-01' AND '1900-12-31' THEN '19th century'
WHEN EventDate BETWEEN '1901-01-01' AND '2000-12-31' THEN '20th century'
WHEN EventDate BETWEEN '2001-01-01' AND '2100-12-31' THEN '21th century'
END)
--------------------------------------------------------
WiseOwl query I believe should be like this:
SELECT
-- derive the century
CASE
WHEN year(e.EventDate) < 1801 THEN '18th century'
WHEN year(e.EventDate) < 1901 THEN '19th century'
WHEN year(e.EventDate) < 2001 THEN '20th century'
ELSE '21st century'
END AS Century,
COUNT(*) AS 'Number events'
FROM
tblEvent AS e
GROUP BY
-- need to group by the century too (the CUBE function shows the grand total too)
CUBE(
CASE
WHEN year(e.EventDate) < 1801 THEN '18th century'
WHEN year(e.EventDate) < 1901 THEN '19th century'
WHEN year(e.EventDate) < 2001 THEN '20th century'
ELSE '21st century'
END
)
ORDER BY
Century
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: | 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.
From: | Andy B |
When: | 23 Mar 20 at 09:50 |
Impressive analysis - thanks!
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 2024. All Rights Reserved.