简体   繁体   中英

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

I've an output like the below.

在此处输入图片说明

I want that to be converted into the below

在此处输入图片说明

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM