Oracle
 sql >> Database >  >> RDS >> Oracle

Come creare pivot dinamici con data e un'altra colonna in Oracle?

Stai cercando di convertire la data e l'identificatore di 2 colonne, quindi hai bisogno di 2 pivot. Prova sotto la query, dovrebbe funzionare. Non esitate a tornare in caso di domande. Funziona in Oracle ma non sono sicuro se stai provando in altri dbms.

select * from (
select * from (
with all_data as 
(select 'NAME1' fname, 123 value, 'ATR' identif, '01-OCT-2018' mydate from dual union all
select 'NAME1' fname, 333 value, 'QTDE' identif, '01-OCT-2018'  mydate from dual union all
select 'NAME2' fname, 212 value, 'ATR' identif, '01-AUG-2018'  mydate from dual union all
select 'NAME2' fname, 123 value, 'QTDE' identif, '01-AUG-2018'  mydate from dual union all
select 'NAME2' fname, 133 value, 'ATR' identif, '01-SEP-2018'  mydate from dual union all
select 'NAME2' fname, 123 value, 'QTDE' identif, '01-SEP-2018'  mydate from dual union all
select 'NAME3' fname, 678 value, 'ATR' identif, '01-AUG-2018'  mydate from dual union all
select 'NAME3' fname, 123 value, 'QTDE' identif, '01-AUG-2018'  mydate from dual )
select identif,fname,value,mydate from  all_data)
pivot 
(
sum(value)
for mydate in ('01-AUG-2018' as "AUG-2018",'01-SEP-2018' as "SEP-2018",'01-OCT-2018' as "OCT-2018")))
pivot
(
sum("AUG-2018") "08-2018"
,sum("SEP-2018") "09-2018"
,sum("OCT-2018") "10-2018"
for identif in ('ATR' ATR,'QTDE' QTDE))
order by 1;