How to make dynamic pivot in oracle PL SQL

后端 未结 2 1956
谎友^
谎友^ 2021-01-14 04:35

I have a query below: And I want to make this pivot dynamic in procedures

  SELECT *
    FROM (  SELECT tcsd AS Aggregator,
                   country,
              


        
相关标签:
2条回答
  • 2021-01-14 04:44

    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;
    
    0 讨论(0)
  • 2021-01-14 04:57

    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;
    
    0 讨论(0)
提交回复
热议问题