• + 0 comments

    Mysql:- pivot is not working in mysql so use case statements, see below

    SELECT 
        MAX(CASE WHEN Occupation = 'Doctor' THEN Name END) AS Doctor,
        MAX(CASE WHEN Occupation = 'Professor' THEN Name END) AS Professor,
        MAX(CASE WHEN Occupation = 'Singer' THEN Name END) AS Singer,
        MAX(CASE WHEN Occupation = 'Actor' THEN Name END) AS Actor
    FROM (
        SELECT *,
               ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY Name) AS RowNum
        FROM OCCUPATIONS
    ) AS NumberedOccupations
    GROUP BY RowNum
    ORDER BY RowNum;