Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Pivot multiple rows to columns

ricard888Mar 21 2021 — edited Mar 21 2021

Hi
With my data_to_pivot I wish to pivot rows for each pair key-value to columns respectively. 
I'm using oracle 11G. Thank you.

--sample data
WITH data_to_pivot AS 
( 
  SELECT date '2021-02-15' as start_date, date '2999-12-31' as end_date, 'Product_Code' dim_name_0, 'XMAS1000' as idx_name_0,'Attribute_1' as dim_name_1, 'Primary' as idx_name_1,'Attribute_2' as dim_name_2 ,'Single Uplink' as idx_name_2,'Attribute_3' as dim_name_3 , 1 as idx_name_3,'Attribute_4' as dim_name_4 , '100 Mbps' as idx_name_4,'Type' as dim_name_5, 'Part_Number' as idx_name_5, 'XMTC001' as string_value FROM DUAL UNION
  SELECT date '2021-02-15' as start_date, date '2999-12-31' as end_date, 'Product_Code' dim_name_0, 'XMAS1000' as idx_name_0,'Attribute_1' as dim_name_1, 'Primary' as idx_name_1,'Attribute_2' as dim_name_2 ,'Single Uplink' as idx_name_2,'Attribute_3' as dim_name_3 , 2 as idx_name_3,'Attribute_4' as dim_name_4 , '200 Gbps' as idx_name_4,'Type' as dim_name_5, 'Part_Number' as idx_name_5 , 'XMTC002' as string_value FROM DUAL UNION
  --
  SELECT date '2021-02-15' as start_date, date '2999-12-31' as end_date, 'Product_Code' dim_name_0, 'XMAS1000' as idx_name_0,'Attribute_1' as dim_name_1, 'Primary' as idx_name_1,'Attribute_2' as dim_name_2 ,'Single Uplink' as idx_name_2,'Attribute_3' as dim_name_3 , 1 as idx_name_3,'Attribute_4' as dim_name_4 , '100 Mbps' as idx_name_4,'Type' as dim_name_5, 'PCMS_Description' as idx_name_5 , 'BT BROADBAND UPLINK Z1 100M' as string_value FROM DUAL UNION
  SELECT date '2021-02-15' as start_date, date '2999-12-31' as end_date, 'Product_Code' dim_name_0, 'XMAS1000' as idx_name_0,'Attribute_1' as dim_name_1, 'Primary' as idx_name_1,'Attribute_2' as dim_name_2 ,'Single Uplink' as idx_name_2,'Attribute_3' as dim_name_3 , 2 as idx_name_3,'Attribute_4' as dim_name_4 , '200 Gbps' as idx_name_4,'Type' as dim_name_5, 'PCMS_Description' as idx_name_5 , 'BT BROADBAND UPLINK Z2 200G' as string_value FROM DUAL
)SELECT *
FROM data_to_pivot 
order by string_value;

Expecting this result
image.png

This post has been answered by Frank Kulash on Mar 21 2021
Jump to Answer
Comments
Post Details
Added on Mar 21 2021
3 comments
7,612 views