oracle 在合并查询中联接多个表

7kjnsjlb  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(124)

我需要在transactions表中获取transaction_dates,以写入名为payment_plan的表中的列。但我需要做这个操作只是为政策,有付款方式为直接借记和现金。所以我建立了以下查询;

MERGE INTO PAYMENT_PLAN pp
USING
(
SELECT pt.transaction_date
    FROM payment_plan pp
    JOIN transactions pt ON (pp.transaction_id = pt.transaction_id)
    JOIN policy pol ON (PP.POLICY_ID = POL.POLICY_ID)
    WHERE pp.payment_plan_status = 'CLOSED'
    AND POL.PAYMENT_METHOD IN ('Cash','DirectDebit')
    AND pp.bank_clearance_date IS NULL
    AND pp.transaction_id IS NOT NULL
    AND pt.transaction_date IS NOT NULL 
) ta ON (ta.transaction_id = pp.transaction_id)
WHEN MATCHED THEN UPDATE 
SET pp.bank_clearance_date = ta.transaction_date,
pp.effective_date = ta.transaction_date,
ta.receipt_date = ta.transaction_date;

它得到一个“ORA-00904:“TA”.“TRANSACTION_ID”:无效标识符”错误。
那么,我如何才能获得现金和直接借记政策的transaction_date列呢?

3npbholx

3npbholx1#

USING子句的查询中,您只包含SELECT pt.transaction_date,而不包含transaction_id,因此标识符是未知的。如果你想使用该列,那么你必须将其包含在SELECT子句中。

MERGE INTO PAYMENT_PLAN pp
USING (
  SELECT pt.transaction_date,
         pp.transaction_id
  FROM   payment_plan pp
         JOIN transactions pt
         ON (pp.transaction_id = pt.transaction_id)
         JOIN policy pol ON (PP.POLICY_ID = POL.POLICY_ID)
  WHERE  pp.payment_plan_status = 'CLOSED'
  AND    POL.PAYMENT_METHOD IN ('Cash','DirectDebit')
  AND    pp.bank_clearance_date IS NULL
  AND    pp.transaction_id      IS NOT NULL
  AND    pt.transaction_date    IS NOT NULL 
) ta ON (ta.transaction_id = pp.transaction_id)
WHEN MATCHED THEN
  UPDATE 
  SET pp.bank_clearance_date = ta.transaction_date,
      pp.effective_date      = ta.transaction_date,
      ta.receipt_date        = ta.transaction_date;

但是,如果你想更新同一行,那么你可以使用ROWID伪列(这实际上是一个指向该行的指针):

MERGE INTO PAYMENT_PLAN pp
USING (
  SELECT pt.transaction_date,
         pp.ROWID AS rid
  FROM   payment_plan pp
         JOIN transactions pt
         ON (pp.transaction_id = pt.transaction_id)
         JOIN policy pol ON (PP.POLICY_ID = POL.POLICY_ID)
  WHERE  pp.payment_plan_status = 'CLOSED'
  AND    POL.PAYMENT_METHOD IN ('Cash','DirectDebit')
  AND    pp.bank_clearance_date IS NULL
  AND    pp.transaction_id      IS NOT NULL
  AND    pt.transaction_date    IS NOT NULL 
) ta ON (ta.rid = pp.ROWID)
WHEN MATCHED THEN
  UPDATE 
  SET pp.bank_clearance_date = ta.transaction_date,
      pp.effective_date      = ta.transaction_date,
      pp.receipt_date        = ta.transaction_date;

或者您可以将查询简化为:

MERGE INTO PAYMENT_PLAN pp
USING (
  SELECT pt.transaction_date,
         pt.transaction_id
  FROM   transactions pt
         ON (pp.transaction_id = pt.transaction_id)
         JOIN policy pol ON (PP.POLICY_ID = POL.POLICY_ID)
  WHERE  POL.PAYMENT_METHOD IN ('Cash','DirectDebit')
  AND    pt.transaction_date    IS NOT NULL
) ta ON (
    pp.payment_plan_status = 'CLOSED'
AND pp.bank_clearance_date IS NULL
AND pp.transaction_id      = ta.transaction_id
)
WHEN MATCHED THEN
  UPDATE 
  SET bank_clearance_date = ta.transaction_date,
      effective_date      = ta.transaction_date,
      receipt_date        = ta.transaction_date;

相关问题