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!

Unpivot data with column name and data dynamically

mariasJun 13 2013 — edited Jun 13 2013

I am looking for the output like this ( around 50 columns in a table)

empno ename col_name col_data

100     clark    JOB          MANAGER

100     clark    DEPTNO   40

100     clark    SAL         1000             

I am writing like this..

select empno, ename,

         'JOB' as column_name,

         job as column_data  

  from test_emp

union all

  select empno, ename,

         'DEPTNO' as column_name,

         deptno as column_data  

  from test_emp  

union all 

   select empno, ename,

         'SAL' as column_name,

         sal as column_data  

  from test_emp  

Is there any other alternative?

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 11 2013
Added on Jun 13 2013
3 comments
658 views