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

Tabel pivot dengan nilai non-kardinal

Solusi yang tidak memerlukan banyak self-join:

Anda dapat PIVOT pada hasil ROW_NUMBER() fungsi analitik untuk mendapatkan hasil yang Anda inginkan:

SELECT *
FROM   (
  SELECT t.*,
         ROW_NUMBER() OVER ( PARTITION BY test ORDER BY ROWNUM ) AS rn
  FROM   your_table t
)
PIVOT ( MAX( Reader ) AS reader, MAX( result ) AS result FOR rn IN ( 1, 2, 3 ) );

Keluaran :

TEST 1_READER 1_RESULT 2_READER 2_RESULT 3_READER 3_RESULT
---- -------- -------- -------- -------- -------- --------
   1 John          1.6 Jack          5.2
   2 Ursula        2.5 Jack          5.1
   3 Albert        3.0

Atau, cukup gunakan fungsi agregasi:

SELECT test,
       MAX( CASE rn WHEN 1 THEN reader END ) AS reader_1,
       MAX( CASE rn WHEN 1 THEN result END ) AS result_1,
       MAX( CASE rn WHEN 2 THEN reader END ) AS reader_2,
       MAX( CASE rn WHEN 2 THEN result END ) AS result_2,
       MAX( CASE rn WHEN 3 THEN reader END ) AS reader_3,
       MAX( CASE rn WHEN 3 THEN result END ) AS result_3
FROM   (
  SELECT t.*,
         ROW_NUMBER() OVER ( PARTITION BY test ORDER BY ROWNUM ) AS rn
  FROM   your_table t
)
GROUP BY test;


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Oracle SQL - Identifikasi rentang nilai berurutan

  2. 5 Cara Memperbarui Data dengan Subquery di Oracle SQL

  3. Bagaimana Cara Memeriksa Tablespace di Oracle SQL Developer?

  4. Bagaimana cara mengubah XMLTYPE di VARCHAR di ORACLE?

  5. Menggunakan Layanan Heterogen Oracle® dengan Dua Sumber Data ODBC