• + 0 comments

    MS SQL Server

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