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!

dynamic sql results as DBMS_output

User_HQFVTMay 12 2017 — edited May 16 2017

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,35020,30,5020,30,9060,70,80
1211

Thanks,

This post has been answered by Billy Verreynne on May 12 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 13 2017
Added on May 12 2017
9 comments
2,244 views