Skip to content
Advertisement

How do i transpose and grouping in PL/SQL [ORACLE]

I have this table:

USER DEP
USER1 DEP1
USER2 DEP2
USER3 DEP2
USER4 DEP3

But i want to transpose it and group user under DEP.

DEP1 DEP2 DEP3
USER1 USER2 USER4
USER3 NULL NULL

What i have tried following pivot query.

The output is: 00933. 00000 - "SQL command not properly ended".

How do i use PIVOT properly?

Advertisement

Answer

You can give each DEP type a row number and then pivot:

Which, for the sample data:

Outputs:

DEP1  | DEP2  | DEP3 
:---- | :---- | :----
USER1 | USER2 | USER4
null  | USER3 | null 

db<>fiddle here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement