Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use of oracle pivot and unpivot functions

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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));

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report