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

Kesulitan SQL JOIN - sepertinya perlu cara membatasi baris dalam kondisi bergabung

Oracle 9i+, menggunakan ROW_NUMBER:

SELECT x.project_id,
       x.title,
       x.event_date,
       x.event_desc
  FROM (SELECT p.project_id,
               p.title,
               e.event_date,
               e.event_desc,
               ROW_NUMBER() OVER(PARTITION BY p.project_id
                                     ORDER BY e.event_date) AS rank
          FROM PROJECT p
     LEFT JOIN EVENT e ON e.project_fk = p.project_id
                      AND e.event_type = 301
         WHERE p.project_id IN (101,102,103)) x
 WHERE x.rank = 1

Oracle 9i+, Menggunakan WITH dan ROW_NUMBER:

WITH example AS (
     SELECT p.project_id,
            p.title,
            e.event_date,
            e.event_desc,
            ROW_NUMBER() OVER(PARTITION BY p.project_id
                                  ORDER BY e.event_date) AS rank
       FROM PROJECT p
  LEFT JOIN EVENT e ON e.project_fk = p.project_id
                   AND e.event_type = 301
      WHERE p.project_id IN (101,102,103))
SELECT x.project_id,
       x.title,
       x.event_date,
       x.event_desc
  FROM example x
 WHERE x.rank = 1


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Apa tujuan dari opsi logging/nologging di Oracle?

  2. Bagaimana Anda menafsirkan rencana penjelasan kueri?

  3. ORA-00907 saat membuat tampilan secara dinamis di PL/SQL dan menggunakan CLOB

  4. Apakah ada cara otomatis untuk menghasilkan skrip rollback saat memasukkan data dengan LINQ2SQL?

  5. Menghubungkan ke database Oracle menggunakan SQLAlchemy