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!

Convert columns into row of a table

User_40B57Apr 19 2021

Need to convert columns into row for the below mentioned table
Scripts:
CREATE TABLE OPTIM.EMP
(
EMP_NO NUMBER,
ENAME VARCHAR2(100 BYTE),
DNO NUMBER
);
select * from emp;
image.png

Query used to convert columns to row is as below:
SELECT dno, LISTAGG(ename,',') WITHIN GROUP (ORDER BY ename) AS employees FROM emp GROUP BY dno ORDER BY dno;
Got output as below :
image.png

But my expected output should be as below:
image.png

I want my data to be displayed in separate cell (Highlighted in Green color).
Basically delimiter need to be replaced with next cell.
I have also tried with PIVOT it doesn't worked.
SELECT *
FROM (SELECT emp_no,ename,dno
FROM emp)
PIVOT (MAX(ename) FOR dno IN (30));

Comments
Post Details
Added on Apr 19 2021
6 comments
124 views