PIVOT
Przykład:
SELECT * from
(
select
kol1,
kol2
from tab1
where kol2 in('wart1','wart2','wart3')
)
PIVOT
(
count(*) for kol2 in('wart1','wart2','wart3')
)
UNPIVOT
Przykład (źródło: kurs SQL Udemy):
Utworzenie widoka z pivota
create view widok_pivot
as
select * from
(
select
department_id,
job_id
from employees
where job_id in('FI_ACCOUNT','PU_CLERK','IT_PROG','MK_REP','MK_MAN')
)
PIVOT
(
count(*) FOR job_id in('FI_ACCOUNT','PU_CLERK','IT_PROG','MK_REP','MK_MAN' )
)
Unpivot
select * from (
select department_id, "'FI_ACCOUNT'","'PU_CLERK'","'IT_PROG'" from widok_pivot)
unpivot
(
(result) for job_id in ("'FI_ACCOUNT'" as 'FI_ACCOUNT', "'PU_CLERK'" as 'PU_CLERK', "'IT_PROG'" as 'IT_PROG')
)