Home » SQL & PL/SQL » SQL & PL/SQL » Unpivot Query Help to transpose the data (Oracle 11g)
Unpivot Query Help to transpose the data [message #653725] Sat, 16 July 2016 12:15 Go to next message
arifs3738
Messages: 39
Registered: November 2015
Location: India
Member
Input

C1     C2      C3      C4
A	1	2	3
B	4	5	6

Table Script:
CREATE TABLE A1
   (	C1 VARCHAR2(5), 
	C2 NUMBER, 
	C3 NUMBER, 
        C4 NUMBER
   );

INSERT INTO A1 VALUES ('A',1,2,3);
INSERT INTO A1 VALUES ('B',4,5,6);
COMMIT;

I want below output...please help me in writing SQL query....

Output

C1     C2
A      B
1      4
2      5
3      6
Re: Unpivot Query Help to transpose the data [message #653726 is a reply to message #653725] Sat, 16 July 2016 12:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
SQL> with lines as (select level line from dual connect by level <= 4),
  2       data as (select a1.*, row_number() over (order by c1) rn from a1)
  3  select max(decode(rn, 1, decode(line, 1,c1, 2,c2, 3,c3, 4,c4))) c1,
  4         max(decode(rn, 2, decode(line, 1,c1, 2,c2, 3,c3, 4,c4))) c2
  5  from lines, data
  6  group by line
  7  order by line
  8  /
C1    C2
----- -----
A     B
1     4
2     5
3     6

4 rows selected.
Re: Unpivot Query Help to transpose the data [message #653731 is a reply to message #653726] Sun, 17 July 2016 12:19 Go to previous message
arifs3738
Messages: 39
Registered: November 2015
Location: India
Member
Thanks Michel
Previous Topic: ORA-01722 when returning ROWTYPE in function
Next Topic: heirarchy between tables
Goto Forum:
  


Current Time: Thu Apr 18 23:16:22 CDT 2024