Initial table students
NAME School Class
John Hs English
Steve Hs Maths
Matthew Hs Science
Jim Hs History
Output Needed: I need the query to auto pick up Name column data from initial table and change it to column headers in output and since the names will continuously change i cannot hard code the names using simple pivot query. I am new to pivot queries so I wanted to request if someone can help me out. Thank You.
School John Steve Matthew Jim
Hs English Maths Science History
Here's what i tried: *Note( I am trying to use this query in Oracle Sql Developer to achieve the output format)
declare
sqlqry clob;
cols clob;
begin
select listagg('''' || NAME || ''' as "' || NAME || '"', ',') within group (order by NAME)
into cols
from (select distinct NAME from Students);
sqlqry :=
'
select * from(select NAME,SCHOOL,CLASS from Students)
pivot(MAX(CLASS) FOR NAME IN (' || cols || ')
)';
execute immediate sqlqry;
end;