How to make dynamic pivot in oracle PL SQL

扶醉桌前 提交于 2019-12-01 06:31:40

Change your proc like below

          BEGIN
             -- Use another variable and initialize with count(*) from prev_month (say totalCount)
             -- Initialize another counter say curCount = 0
             -- 
             FOR x IN (select time_stamp from prev_month)

             LOOP
                -- increment curCount. If curCount = totalCount 
                -- then use 
                --  l_query := l_query|| REPLACE (' TO_DATE(''$X$'',''yyyymmdd'') as DAY_$X$_TOTAL ','$X$',x.time_stamp);   --your code without comma at the end.
                -- else 
                l_query := l_query|| REPLACE (' TO_DATE(''$X$'',''yyyymmdd'') as DAY_$X$_TOTAL, ','$X$',x.time_stamp);
               -- end if.
             END LOOP;

EDIT: Exact syntax

           BEGIN
                 curCount := 0;
                 SELECT COUNT (*) INTO o_count FROM prev_month; 
                 FOR x IN (select time_stamp from prev_month)

                 LOOP
                    curCount := curCount +1; -- increment curCount. 
                    IF curCount = o_count THEN
                         l_query :=l_query|| REPLACE (' TO_DATE(''$X$'',''yyyymmdd'') as DAY_$X$_TOTAL ','$X$',x.time_stamp); 
                    else 
                         l_query := l_query|| REPLACE (' TO_DATE(''$X$'',''yyyymmdd'') as DAY_$X$_TOTAL, ','$X$',x.time_stamp);
                   end if.
                 END LOOP;

Or use RTRIM:

            CREATE OR REPLACE PROCEDURE intl_sms_aggr (p_cursor IN OUT SYS_REFCURSOR)
            AS
               l_query   LONG :=  'SELECT * FROM (  SELECT tcsd AS Aggregator,
                               country,
                               SUM (COUNT) AS total,
                               COUNT (dest_addr) AS bnum,
                               time_stamp
                          FROM t_raw_intl_sms_aggr
                      GROUP BY tcsd,
                               COUNT,
                               country,
                               time_stamp
                      ORDER BY tcsd,
                               COUNT,
                               country,
                               time_stamp) PIVOT (SUM (total) AS total,
                                                 COUNT (bnum) AS bnum_total
                                           FOR time_stamp
                                           IN (';
            BEGIN
               FOR x IN (select time_stamp from prev_month)
               LOOP
                  l_query := l_query|| REPLACE (' TO_DATE(''$X$'',''yyyymmdd'') as DAY_$X$_TOTAL, ','$X$',x.time_stamp);

               END LOOP;
                l_uqery := RTRIM(l_query,',');

               l_query := l_query || ')) ORDER BY aggregator, country';

              -- OPEN p_cursor FOR l_query;
                                   DBMS_OUTPUT.put_line ('query: ' || l_query);
                                   END;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!