We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Using DB2 this is almost correct and I can't figure out how to fix it.
SELECT
MAX(CASE WHEN occupation = 'Doctor' THEN name END) AS Doctor,
MAX(CASE WHEN occupation = 'Professor' THEN name END) AS Professor,
MAX(CASE WHEN occupation = 'Singer' THEN name END) AS Singer,
MAX(CASE WHEN occupation = 'Actor' THEN name END) AS Actor
FROM
(
SELECT name, occupation, ROW_NUMBER() OVER (PARTITION BY occupation ORDER BY name) AS row_num
FROM OCCUPATIONS
) subquery
GROUP BY row_num
ORDER BY row_num;
-OUTPUT-
Aamina Ashley Christeen Eve
Julia Belvet Jane Jennifer
Priya Britney Jenny Ketty
- Maria Kristeen Samantha
- Meera - -
- Naomi - -
- Priyanka - -
Does anyone know why it is outputting a - instead of NULL? Adding 'else NULL' to the case statements does not work.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Occupations
You are viewing a single comment's thread. Return to all comments →
Using DB2 this is almost correct and I can't figure out how to fix it.
SELECT MAX(CASE WHEN occupation = 'Doctor' THEN name END) AS Doctor, MAX(CASE WHEN occupation = 'Professor' THEN name END) AS Professor, MAX(CASE WHEN occupation = 'Singer' THEN name END) AS Singer, MAX(CASE WHEN occupation = 'Actor' THEN name END) AS Actor FROM ( SELECT name, occupation, ROW_NUMBER() OVER (PARTITION BY occupation ORDER BY name) AS row_num FROM OCCUPATIONS ) subquery GROUP BY row_num ORDER BY row_num;
-OUTPUT- Aamina Ashley Christeen Eve Julia Belvet Jane Jennifer Priya Britney Jenny Ketty - Maria Kristeen Samantha - Meera - - - Naomi - - - Priyanka - -
Does anyone know why it is outputting a - instead of NULL? Adding 'else NULL' to the case statements does not work.