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!

once again pivoting (row to column)..

824061Mar 15 2011 — edited Mar 17 2011
Experts, Need help in this (11g database)
Best to explain with data:

CREATE TABLE TEST991 (CODE VARCHAR2(30),MEANING VARCHAR2(30));
INSERT INTO TEST991 VALUES ('A1','ATT5');
INSERT INTO TEST991 VALUES ('A2','ATT4');
INSERT INTO TEST991 VALUES ('A3','ATT2');
INSERT INTO TEST991 VALUES ('A4','ATT3');
INSERT INTO TEST991 VALUES ('A5','ATT1');
COMMIT;

Need output like below one row as data and rows converted to columns:

ATT1 ATT2 ATT3 ATT4 ATT5
A5 A3 A4 A2 A1

I tried PIVOT :

SELECT att1 ,att2 ,att3 ,att4 ,att5
FROM (SELECT code, ROW_NUMBER () OVER (ORDER BY code) AS
r_num
FROM apps.TEST991
)
PIVOT ( MIN (code)
FOR r_num
IN (
1 AS att1,
2 AS att2,
3 AS att3,
4 AS att4,
5 AS att5
)
);

But this gives output as:


ATT1 ATT2 ATT3 ATT4 ATT5
A1 A2 A3 A4 A5

Thanks
BS

Edited by: Bhushan on Mar 16, 2011 11:57 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 14 2011
Added on Mar 15 2011
6 comments
150 views