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.
- Prepare
- SQL
- Advanced Select
- Occupations
- Discussions
Occupations
Occupations
Sort by
recency
|
2043 Discussions
|
Please Login in order to post a comment
with cte as ( select *,ROW_NUMBER() over (partition by occupation order by name asc) as r from occupations ), doc as (select name,r from cte where occupation = 'Doctor'), prof as (select name,r from cte where occupation = 'Professor'), si as (select name,r from cte where occupation = 'Singer'), act as (select name,r from cte where occupation = 'Actor') select doc.name,prof.name,si.name,act.name from prof left join doc on prof.r=doc.r left join si on prof.r=si.r left join act on prof.r=act.r
Here's my disgraceful clunky code -
The solution is at this Carbon link
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;