oracle pivot 和 unpivot 函數的使用
阿新 • • 發佈:2018-07-26
greate 列轉行 格式 cert list nth 字段類型 query apr pivot的格式
select from
( inner_query)
pivot(aggreate_function for pivot_column in ( list of values))
order by ...;
用法舉例:
select
from (
select month,prd_type_id,amount
from all_sales
)
pivot (sum(amount) for month in (1 as JAN,2 as FEB,3 as MAR,4 as APR)
)
order by prd_type_id from all_sales
)
pivot(sum(amount) for (month,prd_type_id) in (
(1,2) as JAN_P2,(2,3) as FEB_P3)
);
select from
( inner_query)
pivot(aggreate_function for pivot_column in ( list of values))
order by ...;
用法舉例:
select
from (
select month,prd_type_id,amount
from all_sales
)
pivot (sum(amount) for month in (1 as JAN,2 as FEB,3 as MAR,4 as APR)
)
order by prd_type_id
轉換多個列
select * from
(select month,prd_type_id,amount
)
pivot(sum(amount) for (month,prd_type_id) in (
(1,2) as JAN_P2,(2,3) as FEB_P3)
);
在轉換中使用多個聚合函數
select * from (select cust_no,mag_man_cert_type,t.mag_man_cert_no,mag_man_type from L_CIF_ENT_CUST_MAG_MAN_INFO t
pivot (max(mag_man_cert_NO) as no ,max(mag_man_cert_type) as type for mag_man_type In (‘01‘ as GLR01,‘02‘ as GLR02,‘03‘ as GLR03));
unpivot可以實現列轉行,所轉的列的字段類型必須一致
unpivot 的用法舉例:
select * from PIVOT_SALES_DATE
unpivot (amount for month in (JAN,FEB,MAR,APR));
oracle pivot 和 unpivot 函數的使用