Skip to content
Advertisement

Transpose in ORACLE SQL (Convert 6 columns into 3 Columns)

I’ve an output like the below.

enter image description here

I want that to be converted into the below

enter image description here

Where ID is the count of TOTAL, HIGH, SENT, WAITING, DATE. I have been contemplating for a while and couldn’t get what I want. Can any one please help in ORACLE SQL?

Thanks in advance.

Advertisement

Answer

Here’s one option:

SQL> with test (cdate, total, high, sent, waiting, loc) as
  2    (select 1012018, 23, 4, 35, 45, 13456 from dual union all
  3     select 1212018, 74, 2, 77, 82, 98765 from dual
  4    ),
  5  temp as
  6    (select 5 rn, loc, cdate as value from test union all
  7     select 1 rn, loc, total          from test union all
  8     select 2 rn, loc, high           from test union all
  9     select 3 rn, loc, sent           from test union all
 10     select 4 rn, loc, waiting        from test
 11    )
 12  select rn, value, loc
 13  from temp
 14  order by loc, rn;

        RN      VALUE        LOC
---------- ---------- ----------
         1         23      13456
         2          4      13456
         3         35      13456
         4         45      13456
         5    1012018      13456
         1         74      98765
         2          2      98765
         3         77      98765
         4         82      98765
         5    1212018      98765

10 rows selected.

SQL>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement