Oracle 18c-联接选择最新记录

时间:2019-09-05 20:28:48

标签: sql oracle greatest-n-per-group

我有两个表:

CLIENT (CUSTOMER_ID NUMBER PK,
        ACTIVE_CLIENT VARCHAR2(1))

CLIENT_HIST (CUSTOMER_ID NUMBER PK,
             PURCHASE_DATE DATE PK,
             PAYMENT_COMPLETE VARCHAR2(1))

我的查询:

   SELECT *
   FROM CLIENT c
   JOIN CLIENT_HIST ch
     ON ch.customer_id = c.customer_id 
    AND ch.purchase_date = (SELECT MAX(purchase_date) 
                            FROM client_hist
                            WHERE customer_id = c.customer_id);
   WHERE c.active_client = 'Y'
     AND NVL(ch.payment_complete,'U') <> 'Y';

基本上,我想要列出所有最近购买时尚未完成付款的活跃客户的列表。就性能而言,这是编写查询的最佳方法吗?

0 个答案:

没有答案