• + 0 comments

    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