Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Pivot dinamis di Oracle sql - Prosedur

Jika Anda memiliki tabel yang ingin Anda sisipkan hasil pivot setiap kali Anda memanggil Stored Proc, Anda dapat menggunakan ini:

CREATE OR REPLACE PROCEDURE dynamic_pivot
AS
  v_sql LONG       := NULL;
  v_statement LONG := NULL;
BEGIN
  SELECT DISTINCT LISTAGG(''''
    || myLetter
    || ''' AS '
    || myLetter,',') WITHIN GROUP (
  ORDER BY myLetter) AS temp_in_statement
  INTO v_statement
  FROM
    (SELECT DISTINCT myLetter FROM test_data
    );
  v_sql := 'insert into pivot_table    
      select * from (SELECT myNumber, myLetter, myValue FROM test_data    
      )    
      PIVOT    
      (      
      SUM(myValue) AS val FOR myLetter IN (' || v_statement || ')    
      )';
  EXECUTE immediate v_sql;
END;



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Lewati dan kembalikan objek array khusus di ibatis dan Oracle di java

  2. Paksa Oracle Jatuhkan Tabel Temp Global

  3. Oracle:'=ANY()' vs. 'IN ()'

  4. Oracle DROP TABLE JIKA ADA Alternatif

  5. Batch beberapa pernyataan pilih saat memanggil Oracle dari ADO.NET