sql >> Database teknologi >  >> RDS >> Oracle

Sådan laver du dynamisk pivot i oracle PL SQL

Skift din proc som nedenfor

          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:Præcis syntaks

           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;


  1. Eksport- og importmetoder for SQL Server-databasetabeller

  2. Forkert DateTime Værdi '0000-00-00 00:00:00' - Date_Sub() i at have

  3. PyMySQL returnerer gamle/snapshot-værdier/kører ikke forespørgsel igen?

  4. En datamodel til en vejrapp