Hi All,
I am trying to execute the below query to convert rows to columns by using pivot function. But I am able to get the results using static values in in pivot .
However I am writing dynamic pivot class in the below the query but I couldn't get the result by using the below query but getting only query as out put.
Can anyone help to get the results as my out put.
declare
sql_stmt varchar2(4000);
pivot_clause varchar2(4000);
begin
select listagg('''' || id || ''' as "' || id || '"', ',') within group (order by id)
into pivot_clause
from (select distinct id from dg_emp);
sql_stmt := 'select * from (select id from dg_emp)
pivot (count(*) for id in (' || pivot_clause || '))';
execute immediate sql_stmt;
dbms_output.put_line(sql_stmt);
end;
/
Getting out put ;
select * from (select id from dg_emp)
pivot (count(*) for id in ('150,250,350' as "150,250,350",'20,30,50' as "20,30,50",'20,30,90' as "20,30,90",'60,70,80' as "60,70,80"))
expected out put;
15,02,50,350 | 20,30,50 | 20,30,90 | 60,70,80 |
1 | 2 | 1 | 1 |
Thanks,