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.
DB2 Solution:
with tb1 as (SELECT
max(CASE WHEN OCCUPATION = 'Doctor' THEN NAME END) Doctor,
max(CASE WHEN OCCUPATION = 'Professor' THEN NAME END) Professor,
max(CASE WHEN OCCUPATION = 'Singer' THEN NAME END) Singer,
max(CASE WHEN OCCUPATION = 'Actor' THEN NAME END) Actor
FROM
(SELECT NAME, OCCUPATION, ROW_NUMBER() OVER (PARTITION BY OCCUPATION ORDER BY NAME) row_num
FROM OCCUPATIONS)
GROUP BY row_num
ORDER BY row_num)
select COALESCE(doctor,'Null'), COALESCE(professor,'Null'), COALESCE(singer,'Null'), COALESCE(actor,'Null')
from tb1;
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 →
DB2 Solution: with tb1 as (SELECT max(CASE WHEN OCCUPATION = 'Doctor' THEN NAME END) Doctor, max(CASE WHEN OCCUPATION = 'Professor' THEN NAME END) Professor, max(CASE WHEN OCCUPATION = 'Singer' THEN NAME END) Singer, max(CASE WHEN OCCUPATION = 'Actor' THEN NAME END) Actor FROM (SELECT NAME, OCCUPATION, ROW_NUMBER() OVER (PARTITION BY OCCUPATION ORDER BY NAME) row_num FROM OCCUPATIONS) GROUP BY row_num ORDER BY row_num) select COALESCE(doctor,'Null'), COALESCE(professor,'Null'), COALESCE(singer,'Null'), COALESCE(actor,'Null') from tb1;