Home » SQL & PL/SQL » SQL & PL/SQL » create separate record for each column (Oracle 11g, Linux)
create separate record for each column [message #616514] Tue, 17 June 2014 14:44 Go to next message
rahul1982
Messages: 53
Registered: November 2011
Location: Pune
Member
Hi,

I have a below table -

ROLL_NO          NAME 		ADDRESS
1		 JAI		USA	
2		 CHRIS  	UK
3		 JASMINE 	INDIA
4		 MURTHY  	USA


i want to display the output as shown below -

COLUMNNAME	VALUE   ROLL_NO
ADDRESS	        USA	1
NAME	        JAI	1
ROLL_NO	        1	1
ADDRESS	        UK	2
NAME	        CHRIS	2
ROLL_NO	        2	2
ADDRESS	        IND	3
NAME	        JASMINE	3
ROLL_NO	        3	3
ADDRESS  	AUS	4
NAME	        MURTHY	4
ROLL_NO	        4	4


SQL -

 select 'ROLL_NO' COLUMNNAME ,ROLL_NO VALUE, ROLL_NO from TEST_TABLE 
 UNION
 select 'NAME', NAME,ROLL_NO from TEST_TABLE 
 UNION
 select 'ADDRESS',ADDRESS,ROLL_NO from TEST_TABLE 
 ORDER BY 3,1 


Union is taking time as the data volume is huge in real table, please suggest where will be the alternative to get same output.

Thank you!!!
Re: create separate record for each column [message #616515 is a reply to message #616514] Tue, 17 June 2014 14:52 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
See both replies from me and Michel here http://www.orafaq.com/forum/mv/msg/193056/615561/#msg_615561

If it doesn't resolve your issue, then please post the CREATE and INSERT statements to replicate your issue and provide a solution.
Re: create separate record for each column [message #616517 is a reply to message #616514] Tue, 17 June 2014 14:56 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
rahul1982 wrote on Wed, 18 June 2014 01:14

Union is taking time as the data volume is huge in real table, please suggest where will be the alternative to get same output.


Tuning the query would be the next step, it needs more details as you haven't posted any helpful information regarding performance. First follow the above mentioned suggestion, if you face issues, please let us know.

By the way, UNION imposes selection of distinct dataset and thus requires more resource, thus it might result in taking some more time.
Re: create separate record for each column [message #616525 is a reply to message #616514] Tue, 17 June 2014 19:09 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
SQL> select  *
  2    from  tbl
  3  /

   ROLL_NO NAME    ADDRE
---------- ------- -----
         1 JAI     USA
         2 CHRIS   UK
         3 JASMINE INDIA
         4 MURTHY  USA

SQL> with l as (
  2             select  'ADDRESS' columnname from dual union all
  3             select  'NAME' from dual union all
  4             select  'ROLL_NO' from dual
  5            )
  6  select  columnname,
  7          case columnname
  8            when 'ADDRESS' then address
  9            when 'NAME' then name
 10            else to_char(roll_no)
 11          end value,
 12          roll_no
 13    from  tbl,
 14          l
 15    order by roll_no,
 16             columnname
 17  /

COLUMNNAME VALUE         ROLL_NO
---------- ---------- ----------
ADDRESS    USA                 1
NAME       JAI                 1
ROLL_NO    1                   1
ADDRESS    UK                  2
NAME       CHRIS               2
ROLL_NO    2                   2
ADDRESS    INDIA               3
NAME       JASMINE             3
ROLL_NO    3                   3
ADDRESS    USA                 4
NAME       MURTHY              4

COLUMNNAME VALUE         ROLL_NO
---------- ---------- ----------
ROLL_NO    4                   4

12 rows selected.

SQL>


SY.
Re: create separate record for each column [message #616542 is a reply to message #616514] Wed, 18 June 2014 02:31 Go to previous message
rahul1982
Messages: 53
Registered: November 2011
Location: Pune
Member
Thank you!!
Previous Topic: Looping or SQL
Next Topic: How to print vertical numbers
Goto Forum:
  


Current Time: Fri Apr 19 12:04:24 CDT 2024