• + 0 comments

    MS SQL SERVER

    WITH ordered AS ( SELECT Name, Occupation, ROW_NUMBER() OVER(PARTITION BY Occupation ORDER BY Name) AS rn FROM OCCUPATIONS ), ctc as ( SELECT * FROM ( SELECT Name, Occupation, rn FROM ordered ) AS src PIVOT ( MAX(Name) FOR Occupation IN ([Doctor], [Professor], [Singer], [Actor]) ) AS p ) SELECT Doctor, Professor, Singer, Actor FROM ctc ORDER BY rn;