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!

Convert column values of a table into row value of another table

Azadare MMar 16 2015 — edited Mar 17 2015

Hi All,

I have table test_psn which having 7 columns all are varchar2:

create table test_psn(code varchar2(20), first_yesr varchar2(20), val1 varchar2(20),

val2 varchar2(20), val3 varchar2(20), val4 varchar2(20), val5 varchar2(20));

insert into test_psn values ('P0021745','12.25','2.99','16.14','23.04','11.98',);

insert into test_psn values ('P0017106','-6.8','34.1','20.45','2.81','6.94',);

insert into test_psn values ('P0017128','-4.83','-999','18.17','7','9.4',);

insert into test_psn values ('P0018255','1.48','-3.26','5.2','1.9','1.15',);

insert into test_psn values ('P0018263','1.48','-3.26','','1.9','1.15',);

insert into test_psn values ('P0018246','1.48','-3.26','5.2','1.9','1.15',);

insert into test_psn values ('P0000004','','-1.2','-5.75','17.3','-3.21',);

insert into test_psn values ('P0000005','2.43','-1.71','-6.58','14.93','-5.29',);

insert into test_psn values ('P0000010','4.19','-999','-5.47','16.03','-4.3',);

insert into test_psn values ('P0022677','2.36','-0.45','-11.55','7.04','4.53',);

below is the sample data present in above table:

  

CODEFIRSTYEARVAL1VAL2VAL3VAL4VAL5
P0021745201012.252.9916.1423.0411.98
P00171062009-6.834.120.452.816.94
P00171282009-4.83-99918.1779.4
P001825520041.48-3.265.21.91.15
P001826320041.48-3.261.91.15
P001824620041.48-3.265.21.91.15
P00000042004-1.2-5.7517.3-3.21
P000000520042.43-1.71-6.5814.93-5.29
P000001020044.19-999-5.4716.03-4.3
P002267720112.36-0.45-11.557.044.53

Now I want create a new table which will have only threecolumns like

create table main_table(code varchar2(20), processed_date date, returns number);

Logic is to insert rows into main_table from test_psn table is below:

Code column's value should be the same in both tables, processed_date value should be a proper date like :

if firstyear column value of test_psn table for particular code is 2010 then our processed_date values should be '31-Mar-2010' and corresponding returns column's values should be val1 values.

here processed_date always start with 31-Mar  and year should be firstyear columns's value. and for each val column values processed date should increased by one quarter, below is the sample output for 'P0021754' code:

  

codeprocessed_datereturns
P002174531-Mar-1012.25
P002174630-Jun-102.99
P002174730-Sep-1016.14
P002174831-Dec-1023.04
P002174931-Mar-1111.98

Here if the value of any val column is either null or -99 then in that case we need to skip that quarter, below is sample data for P0017128 code:

  

P001712831-Mar-09-4.83
P001712930-Sep-0918.17
P001713031-Mar-107
P001713130-Sep-109.4

Thanks,

This post has been answered by odie_63 on Mar 17 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 14 2015
Added on Mar 16 2015
5 comments
1,290 views