sql >> データベース >  >> RDS >> Oracle

SQLJOINの難しさ-結合条件で行を制限する方法が必要なようです

    Oracle 9i +、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 +、WITHおよび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. 一意の制約、重複を回避する方法

    2. Oracleの数値フォーマット要素のリスト

    3. JavaのSalesforceSOQL

    4. キーで集計するときに、従属列を `GROUP BY`から除外できないのはなぜですか?