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
551 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 ...
This question is tricky because there are many ways you can do it. In fact, you don't even have to use "null traps", nor all the aliases and joins mentioned in the problem. However, I am not 100% sure this is the way Wise Owl wanted it to be done nor am I sure this is correct. Proceed with caution!
ISNULL(Expression, 'All categories') Function
Checks if any of the arguments in the function is NULL, in this case whether if there is a family of a higher hierarchy, and returns the Top Hierarchy 'All categories'.
Note: The program reads the hierarchy from Lowest->Highest. If there are only two levels the TopFamily becomes NULL and if only one level the TopFamily and ParentFamily become NULL , so on. Family < Parent Family < Top Family [Lowest< Middle < Highest].
CASE
I used a CASE here to check if ONLY the TopFamily is NULL, if it's NULL it will return '' and if it's not NULL it will return the rest of the function if it exists. I did this so that the ISNULL() function does not make every row with less than 3 levels of hierarchy = 'All categories'.
The rest of the function is simple, a basic string calculation to add the remainder of the FamilyNames in the hierarchy.
LEFT JOIN
TopFamily being the highest in the hierarchy, I set it as ParentFamily's ParentFamilyID and Family's ParentFamilyID as ParentFamily's FamilyID. Thus, TopFamily.FamilyID = ParentFamily.ParentFamilyID and Family.ParentFamilyID = ParentFamily.FamilyID.
You can see this more clearly by executing the code below, where I wrote it so that it generates all the revelant FamilyIDs and ParentFamilyIDs
---SPOILER [ANSWER]---
SELECT
---FOR CLARITY---
TopFamily.FamilyID as [TopFamily FamilyID],
ParentFamily.ParentFamilyID as [ParentFamily ParentFamilyID],
ParentFamily.FamilyID as [ParentFamily FamilyID],
Family.ParentFamilyId as [Family ParentFamilyID],
Family.FamilyID as [Family FamilyID],
Family.FamilyName,
ISNULL(
CASE
WHEN TopFamily.FamilyName IS NULL
THEN ''
ELSE TopFamily.FamilyName + ' > '
END + ParentFamily.FamilyName + ' > ' + Family.FamilyName
, 'All categories'
) as 'Family path'
FROM
tblFamily as family
LEFT JOIN
tblFamily as ParentFamily
ON ParentFamily.FamilyID = Family.ParentFamilyID
LEFT JOIN
tblFamily as TopFamily
ON TopFamily.FamilyId = ParentFamily.ParentFamilyID
ORDER BY
FamilyName
For Amy's choice my enemy name says 'Winifred GillyFlower'.
NOTE: The QUESTION IS ORDERD BY EVENTDATE, if anyone was wondering the order.
If anyone could verify if this is correct, because it seem uncomfortably long:
SELECT
EventName,
CASE
WHEN LEFT(EventName, 1) = RIGHT(EventName, 1)
THEN 'Same letter'
WHEN (LEFT(EventName, 1) = 'a' OR LEFT(EventName, 1) = 'e' OR LEFT(EventName, 1) = 'i' OR LEFT(EventName, 1) = 'o' OR LEFT(EventName, 1) = 'u') AND (RIGHT(EventName, 1) = 'a' OR RIGHT(EventName, 1) = 'e' OR RIGHT(EventName, 1) = 'i' OR RIGHT(EventName, 1) = 'o' OR RIGHT(EventName, 1) = 'u')
THEN 'Begins and ends with vowel'
END as 'Verdict'
FROM
tblEvent
WHERE (CASE
WHEN LEFT(EventName, 1) = RIGHT(EventName, 1)
THEN 'Same letter'
WHEN (LEFT(EventName, 1) = 'a' OR LEFT(EventName, 1) = 'e' OR LEFT(EventName, 1) = 'i' OR LEFT(EventName, 1) = 'o' OR LEFT(EventName, 1) = 'u') AND (RIGHT(EventName, 1) = 'a' OR RIGHT(EventName, 1) = 'e' OR RIGHT(EventName, 1) = 'i' OR RIGHT(EventName, 1) = 'o' OR RIGHT(EventName, 1) = 'u')
THEN 'Begins and ends with vowel'
END ) IS NOT NULL
ORDER BY
EventDate
--SPOILER--
ANSWER:
SELECT
Country
, KmSquared
, ROUND(KmSquared/20761, 0) as 'WalesUnits'
, (Kmsquared % 20761) as 'AreaLeftOver'
, CASE
WHEN
CAST(ROUND((KmSquared/20761), 0) as varchar(10)) < 1
THEN
'Smaller than Wales'
ELSE
CAST(ROUND((KmSquared/20761), 0) as VARCHAR(10)) + ' x ' + ' Wales plus ' +
CAST( (KmSquared % 20761) as VARCHAR(10)) + ' sq. km.'
END as 'Wales Comparison'
FROM
CountriesByArea
Order By ABS(KmSquared - 20761)
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.