언어

mysql에 pivot 없음

땅호720 2024. 6. 14. 14:38
with n as (
select *, row_number() over (partition by occupation order by name) rn
from occupations)

select min(if(occupation='Doctor', name, null)) 'Doctor',
        min(if(occupation='Professor', name, null)) 'Professor',
        min(if(occupation='Singer', name, null)) 'Singer',
        min(if(occupation='Actor', name, null)) 'Actor'
from n
group by rn

/*
select Doctor, Professor, Singer, Actor
from n
pivot (
  max(name)
  for occupation in (Doctor, Professor, Singer, Actor)
) as pivotTable
*/