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.
with PivotTable as (
select Name,
occupation,
row_number() over(PARTITION BY occupation order by name) as RowNum
from occupations )
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 PivotTable
group by RowNum;
the first step output:
name | occupation | RowNum
samatha doctor 1
julia actor 1
Maria actor 1
...
because the combinations of name and occupation is unique,
so in the next step use Max( ) or Min( ) can choose the result and filter the NULL value.
Doctor | Actor | Singer | Professor
samantha ashley meera jane
enny ketty null maria
...
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 →
with PivotTable as ( select Name, occupation, row_number() over(PARTITION BY occupation order by name) as RowNum from occupations )
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 PivotTable group by RowNum;
the first step output:
name | occupation | RowNum samatha doctor 1 julia actor 1 Maria actor 1 ...
because the combinations of name and occupation is unique, so in the next step use Max( ) or Min( ) can choose the result and filter the NULL value.
Doctor | Actor | Singer | Professor samantha ashley meera jane enny ketty null maria ...