0

请帮忙!!!我正在使用 SQL Dev 2.1,我想找到最新的日期并加入表格。我的查询如下:-

我想获得最新的 DEBT_COLLECTION_STEPS_V.TRANSACTION_DATE,因为有多个日期与相同的发票号相关,但针对 DEBT_COLLECTION_STEPS_V.DESCRIPTION 发布了不同的评论。

SELECT AR_INVOICE_INQ_V.CLIENT CLIENT,
AR_INVOICE_INQ_V.CLI_NAME CLI_NAME,
AR_INVOICE_INQ_V.PARTNER PARTNER,
AR_INVOICE_INQ_V.PAR_NAME PAR_NAME,
AR_INVOICE_INQ_V.MANAGER MANAGER,
AR_INVOICE_INQ_V.REFERENCE REFERENCE,
AR_INVOICE_INQ_V.STATUS_CODE STATUS_CODE,
AR_INVOICE_INQ_V.INVOICE_DATE INVOICE_DATE,
AR_INVOICE_INQ_V.TIME_BILLED TIME_BILLED,
AR_INVOICE_INQ_V.GROUP_CLIENT_CODE GROUP_CLIENT_CODE,
AR_INVOICE_INQ_V.GRPCLI_NAME GRPCLI_NAME,
AR_INVOICE_INQ_V.INVOICE_BALANCE,
SUBSTR(AR_INVOICE_INQ_V.MANAGER, 3, 2) CENTRE,
DEBT_COLLECTION_STEPS_V.DESCRIPTION,
MAX (BILLING_TRANSACTIONS_V.SEQUENCE_CODE),
DEBT_COLLECTION_STEPS_V.TRANSACTION_DATE
FROM AR_INVOICE_INQ_V
LEFT JOIN DEBT_COLLECTION_STEPS_V
ON DEBT_COLLECTION_STEPS_V.INVOICE_NUMBER = AR_INVOICE_INQ_V.REFERENCE
JOIN BILLING_TRANSACTIONS_V
ON BILLING_TRANSACTIONS_V.INVOICE_OR_CN_NUMBER = AR_INVOICE_INQ_V.REFERENCE
WHERE AR_INVOICE_INQ_V.INVOICE_BALANCE         >0
AND AR_INVOICE_INQ_V.MANAGER BETWEEN 'AAAA' AND 'AZZZ'
GROUP BY AR_INVOICE_INQ_V.CLIENT,
AR_INVOICE_INQ_V.CLI_NAME,
AR_INVOICE_INQ_V.PARTNER,
AR_INVOICE_INQ_V.PAR_NAME,
AR_INVOICE_INQ_V.MANAGER,
AR_INVOICE_INQ_V.REFERENCE,
AR_INVOICE_INQ_V.STATUS_CODE,
AR_INVOICE_INQ_V.INVOICE_DATE,
AR_INVOICE_INQ_V.TIME_BILLED,
AR_INVOICE_INQ_V.GROUP_CLIENT_CODE,
AR_INVOICE_INQ_V.GRPCLI_NAME,
AR_INVOICE_INQ_V.INVOICE_BALANCE,
SUBSTR(AR_INVOICE_INQ_V.MANAGER, 3, 2),
DEBT_COLLECTION_STEPS_V.DESCRIPTION,
DEBT_COLLECTION_STEPS_V.TRANSACTION_DATE;
4

1 回答 1

0

将您的 LEFT JOIN 替换为获取每个 INVOICE_NUMBER 的 MAX(TRANSACTION_DATE) 的查询。

那应该给你你想要的:

select AR_INVOICE_INQ_V.CLIENT CLIENT,
  AR_INVOICE_INQ_V.CLI_NAME CLI_NAME,
  AR_INVOICE_INQ_V.PARTNER PARTNER,
  AR_INVOICE_INQ_V.PAR_NAME PAR_NAME,
  AR_INVOICE_INQ_V.MANAGER MANAGER,
  AR_INVOICE_INQ_V.REFERENCE REFERENCE,
  AR_INVOICE_INQ_V.STATUS_CODE STATUS_CODE,
  AR_INVOICE_INQ_V.INVOICE_DATE INVOICE_DATE,
  AR_INVOICE_INQ_V.TIME_BILLED TIME_BILLED,
  AR_INVOICE_INQ_V.GROUP_CLIENT_CODE GROUP_CLIENT_CODE,
  AR_INVOICE_INQ_V.GRPCLI_NAME GRPCLI_NAME,
  AR_INVOICE_INQ_V.INVOICE_BALANCE,
  SUBSTR(AR_INVOICE_INQ_V.MANAGER, 3, 2) CENTRE,
  DEBT_COLLECTION_STEPS_V.DESCRIPTION,
  MAX(BILLING_TRANSACTIONS_V.SEQUENCE_CODE),
  DC.TRANSACTION_DATE
from AR_INVOICE_INQ_V
left join (SELECT INVOICE_NUMBER, MAX(TRANSACTION_DATE) FROM DEBT_COLLECTION_STEPS_V GROUP BY INVOICE_NUMBER) DC on DC.INVOICE_NUMBER = AR_INVOICE_INQ_V.REFERENCE
join BILLING_TRANSACTIONS_V on BILLING_TRANSACTIONS_V.INVOICE_OR_CN_NUMBER = AR_INVOICE_INQ_V.REFERENCE
where AR_INVOICE_INQ_V.INVOICE_BALANCE > 0
  and AR_INVOICE_INQ_V.MANAGER between 'AAAA' and 'AZZZ'
group by AR_INVOICE_INQ_V.CLIENT,
  AR_INVOICE_INQ_V.CLI_NAME,
  AR_INVOICE_INQ_V.PARTNER,
  AR_INVOICE_INQ_V.PAR_NAME,
  AR_INVOICE_INQ_V.MANAGER,
  AR_INVOICE_INQ_V.REFERENCE,
  AR_INVOICE_INQ_V.STATUS_CODE,
  AR_INVOICE_INQ_V.INVOICE_DATE,
  AR_INVOICE_INQ_V.TIME_BILLED,
  AR_INVOICE_INQ_V.GROUP_CLIENT_CODE,
  AR_INVOICE_INQ_V.GRPCLI_NAME,
  AR_INVOICE_INQ_V.INVOICE_BALANCE,
  SUBSTR(AR_INVOICE_INQ_V.MANAGER, 3, 2),
  DEBT_COLLECTION_STEPS_V.DESCRIPTION,
  DC.TRANSACTION_DATE;
于 2013-10-24T14:58:32.810 回答