Get the App
SLTechnology News&Howtos  ›  Database  › 

The use of oracle pivot and unpivot functions

Shulou Source: shulou.com Published: 2022-06-01 15:05:21 10月03日 Update

Format of pivot

select from

( inner_query)

pivot(aggreate_function for pivot_column in ( list of values))

order by ...;

Examples of usage:

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

Converting multiple columns

select * from

(select month,prd_type_id,amount

from all_sales

)

pivot(sum(amount) for (month,prd_type_id) in (

(1,2) as JAN_P2,(2,3) as FEB_P3)

);

Using multiple aggregate functions in a transformation

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 can realize column rotation, and the field types of the columns to be rotated must be consistent.

Examples of unpivot usage:

select * from PIVOT_SALES_DATE

unpivot (amount for month in (JAN,FEB,MAR,APR));

Tags: Multiple function consistent field format type Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MariaDB Xiaomi Redmi vpn