Solution #1: We pivot every department names under its respective (department) group name using functions ROW_NUMBER (window), MAX, COALESCE and CASE statements. , COALESCE(MAX(CASE WHEN ...