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!

Converting Rows into Columns in oracle 11g

1002123May 25 2016 — edited May 26 2016

Hi,

I am using database "Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production"

I am trying to convert a set of rows in columns in specific way. I am trying to explain the situation with an example below. Can you please help me to achieve this.

CREATE TABLE ROW_COL

(

ID NUMBER(4),

DISC_NM VARCHAR2(200),

DISC_VAL NUMBER,

L_YR NUMBER(4),

H_YR NUMBER(4)

);

Insert into ROW_COL (ID,DISC_NM,DISC_VAL,L_YR,H_YR) values (7587,'DEPR_AMT_F',0.9,2016,2099);

Insert into ROW_COL (ID,DISC_NM,DISC_VAL,L_YR,H_YR) values (7587,'DEPR_AMT_C',1.05,2016,2099);

Insert into ROW_COL (ID,DISC_NM,DISC_VAL,L_YR,H_YR) values (7587,'DEPR_AMT_F',0.91,2009,2016);

Insert into ROW_COL (ID,DISC_NM,DISC_VAL,L_YR,H_YR) values (7587,'DEPR_AMT_C',1.04,2009,2016);

Insert into ROW_COL (ID,DISC_NM,DISC_VAL,L_YR,H_YR) values (7587,'DEPR_AMT_F',0.93,1997,2009);

Insert into ROW_COL (ID,DISC_NM,DISC_VAL,L_YR,H_YR) values (7587,'DEPR_AMT_C',1.03,1997,2009);

Insert into ROW_COL (ID,DISC_NM,DISC_VAL,L_YR,H_YR) values (7587,'DEPR_AMT_F',0.95,1970,1997);

Insert into ROW_COL (ID,DISC_NM,DISC_VAL,L_YR,H_YR) values (7587,'DEPR_AMT_C',1.02,1970,1997);

Commit;

Input data looks like this.

SELECT * FROM ROW_COL;

ID          DISC_NM                                      DISC_VAL L_YR        H_YR

7587DEPR_AMT_F0.9519701997
7587DEPR_AMT_C1.0219701997
7587DEPR_AMT_F0.9319972009
7587DEPR_AMT_C1.0319972009
7587DEPR_AMT_F0.9120092016
7587DEPR_AMT_C1.0420092016
7587DEPR_AMT_F0.920162099
7587DEPR_AMT_C1.0520162099

Desired Output:

ID       L_YR   H_YR   DEPR_AMT_F  DEPR_AMT_C

7587   1970        1997             0.95                    1.02

7587   1997        2009             0.93                    1.03

7587   2009        2016             0.91                    1.04

7587   2016        2099             0.9                      1.05 

This post has been answered by CarlosDLG on May 25 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 23 2016
Added on May 25 2016
6 comments
1,168 views