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