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.
set @r_Doctor = 0, @r_Professor = 0,@r_Singer = 0,@r_Actor = 0;
select min(Doctor),min(Professor),min(Singer),min(Actor) from(
select
case
when OCCUPATION = 'Doctor' then (@r_Doctor:= @r_Doctor + 1)
when OCCUPATION = 'Professor' then (@r_Professor:= @r_Professor + 1)
when OCCUPATION = 'Singer' then (@r_Singer:= @r_Singer + 1)
when OCCUPATION = 'Actor' then (@r_Actor:= @r_Actor + 1)
end as row_num,
case when OCCUPATION = 'Doctor' then name else NULL
end as Doctor,
case when OCCUPATION = 'Professor' then name else NULL
end as Professor,
case when OCCUPATION = 'Singer' then name else NULL
end as Singer,
case when OCCUPATION = 'Actor' then name else NULL
end as Actor
from OCCUPATIONS
order by name
) as tbl
group by row_num;
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 →
set @r_Doctor = 0, @r_Professor = 0,@r_Singer = 0,@r_Actor = 0; select min(Doctor),min(Professor),min(Singer),min(Actor) from( select case when OCCUPATION = 'Doctor' then (@r_Doctor:= @r_Doctor + 1) when OCCUPATION = 'Professor' then (@r_Professor:= @r_Professor + 1) when OCCUPATION = 'Singer' then (@r_Singer:= @r_Singer + 1) when OCCUPATION = 'Actor' then (@r_Actor:= @r_Actor + 1) end as row_num, case when OCCUPATION = 'Doctor' then name else NULL end as Doctor, case when OCCUPATION = 'Professor' then name else NULL end as Professor, case when OCCUPATION = 'Singer' then name else NULL end as Singer, case when OCCUPATION = 'Actor' then name else NULL end as Actor from OCCUPATIONS order by name ) as tbl group by row_num;