Browse 547 attributed reviews, viewable separately for our classroom and online training

# Details for hjay629

hjay629 has participated in the following threads:

### Added by hjay629 on 18 Feb 2019 at 23:28

--SPOILER--

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)

### Added by hjay629 on 19 Feb 2019 at 01:01

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

### Added by hjay629 on 19 Feb 2019 at 21:43

For Amy's choice my enemy name says 'Winifred GillyFlower'.

### Added by hjay629 on 20 Feb 2019 at 06:12

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

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