SELECT AP_INVOICE_DISTRIBUTIONS_ALL.DISTRIBUTION_LINE_NUMBER
,AP_INVOICE_DISTRIBUTIONS_ALL.LAST_UPDATED_BY
,AP_INVOICE_DISTRIBUTIONS_ALL.LAST_UPDATE_DATE
,AP_INVOICE_DISTRIBUTIONS_ALL.DIST_CODE_COMBINATION_ID
,AP_INVOICE_DISTRIBUTIONS_ALL.CREATED_BY
,AP_INVOICE_DISTRIBUTIONS_ALL.CREATION_DATE
,AP_INVOICES_ALL.INVOICE_ID
,AP_INVOICES_ALL.INVOICE_NUM
,AP_INVOICES_ALL.INVOICE_CURRENCY_CODE
,AP_INVOICES_ALL.VENDOR_ID
,AP_INVOICES_ALL.VENDOR_SITE_ID
,AP_INVOICES_ALL.INVOICE_DATE
,AP_INVOICE_DISTRIBUTIONS_ALL.LINE_TYPE_LOOKUP_CODE
,AP_INVOICES_ALL.INVOICE_RECEIVED_DATE
,AP_INVOICES_ALL.CREATION_DATE
,AP_INVOICE_DISTRIBUTIONS_ALL.ORG_ID
,PO_HEADERS_ALL.SEGMENT1
,PO_LINES_ALL.LINE_NUM
,PO_HEADERS_ALL.CREATION_DATE
,AP_INVOICES_ALL.LAST_UPDATE_DATE
,PO_HEADERS_ALL.LAST_UPDATE_DATE
,PO_LINES_ALL.LAST_UPDATE_DATE
,PO_LINES_ALL.ITEM_DESCRIPTION
,AP_INVOICE_DISTRIBUTIONS_ALL.DESCRIPTION
,AP_INVOICES_ALL.INVOICE_TYPE_LOOKUP_CODE
,AP_INVOICES_ALL.WFAPPROVAL_STATUS
,AP_INVOICES_ALL.PAYMENT_STATUS_FLAG
,AP_INVOICES_ALL.CANCELLED_DATE
,AP_INVOICE_DISTRIBUTIONS_ALL.ACCOUNTING_DATE
,PO_DISTRIBUTIONS_ALL.PO_DISTRIBUTION_ID
,AP_INVOICE_DISTRIBUTIONS_ALL.RCV_TRANSACTION_ID
,AP_INVOICE_DISTRIBUTIONS_ALL.MATCH_STATUS_FLAG
,AP_INVOICE_DISTRIBUTIONS_ALL.INVOICE_DISTRIBUTION_ID
,AP_INVOICES_ALL.TERMS_ID
,AP_INVOICE_DISTRIBUTIONS_ALL.EXCHANGE_DATE
,AP_INVOICES_ALL.EXCHANGE_DATE
,AP_INVOICE_DISTRIBUTIONS_ALL.EXCHANGE_RATE_TYPE
,AP_INVOICES_ALL.EXCHANGE_RATE_TYPE
,AP_INVOICE_DISTRIBUTIONS_ALL.EXCHANGE_RATE
,AP_INVOICES_ALL.EXCHANGE_RATE
,AP_INVOICE_DISTRIBUTIONS_ALL.ACCTS_PAY_CODE_COMBINATION_ID
,AP_INVOICES_ALL.ACCTS_PAY_CODE_COMBINATION_ID
,AP_INVOICE_DISTRIBUTIONS_ALL.SET_OF_BOOKS_ID
,AP_INVOICES_ALL.SET_OF_BOOKS_ID
,AP_INVOICE_DISTRIBUTIONS_ALL.QUANTITY_INVOICED
,AP_INVOICE_DISTRIBUTIONS_ALL.AMOUNT
,AP_INVOICE_DISTRIBUTIONS_ALL.BASE_AMOUNT
,CASE WHEN AP_INVOICE_LINES_ALL.UNIT_MEAS_LOOKUP_CODE IS NULL
THEN PO_LINES_ALL.UNIT_MEAS_LOOKUP_CODE
ELSE AP_INVOICE_LINES_ALL.UNIT_MEAS_LOOKUP_CODE
END
,AP_INVOICE_DISTRIBUTIONS_ALL.INVOICE_PRICE_VARIANCE
,AP_INVOICE_DISTRIBUTIONS_ALL.EXCHANGE_RATE_VARIANCE
,PO_VENDORS.RECEIPT_REQUIRED_FLAG
,PO_VENDORS.INSPECTION_REQUIRED_FLAG
,PO_VENDOR_SITES_ALL.PURCHASING_SITE_FLAG
,PO_VENDORS.LAST_UPDATE_DATE
,AP_INVOICES_ALL.SOURCE
,AP_INVOICES_ALL.PROJECT_ID
,AP_INVOICES_ALL.TASK_ID
,AP_INVOICES_ALL.EXPENDITURE_TYPE
,AP_INVOICES_ALL.EXPENDITURE_ORGANIZATION_ID
,AP_INVOICES_ALL.PAYMENT_CURRENCY_CODE
,AP_INVOICES_ALL.PAYMENT_CROSS_RATE
,AP_INVOICES_ALL.REQUESTER_ID
,AP_INVOICE_LINES_ALL.LINE_NUMBER
,AP_INVOICE_LINES_ALL.LINE_TYPE_LOOKUP_CODE
,AP_INVOICE_LINES_ALL.REQUESTER_ID
,AP_INVOICE_LINES_ALL.DESCRIPTION
,AP_INVOICES_ALL.LEGAL_ENTITY_ID
FROM AP_INVOICE_DISTRIBUTIONS_ALL
,AP_INVOICE_LINES_ALL
,AP_INVOICES_ALL
,PO_LINES_ALL
,PO_HEADERS_ALL
,PO_VENDORS
,PO_VENDOR_SITES_ALL
,PO_DISTRIBUTIONS_ALL
,(SELECT AP_INVOICES_ALL.INVOICE_ID
FROM AP_INVOICES_ALL
WHERE AP_INVOICES_ALL.LAST_UPDATE_DATE > TO_DATE(:P_LAST_EXTRACT_DATE,'MM/DD/Y YYY HH24:MI:SS')
UNION
SELECT AP_INVOICE_LINES_ALL.INVOICE_ID
FROM AP_INVOICE_LINES_ALL
WHERE AP_INVOICE_LINES_ALL.LAST_UPDATE_DATE > TO_DATE(:P_LAST_EXTRACT_DATE,'MM /DD/YYYY HH24:MI:SS')
UNION
SELECT DISTINCT AP_HOLDS_ALL.INVOICE_ID
FROM AP_HOLDS_ALL
WHERE AP_HOLDS_ALL.RELEASE_LOOKUP_CODE IS NULL
AND AP_HOLDS_ALL.LAST_UPDATE_DATE > TO_DATE(:P_LAST_EXTRACT_DATE,'MM/DD/YYYY HH24:MI:SS')
UNION
SELECT DISTINCT AP_INVOICE_DISTRIBUTIONS_ALL.INVOICE_ID
FROM PO_DISTRIBUTIONS_ALL, AP_INVOICE_DISTRIBUTIONS_ALL
WHERE AP_INVOICE_DISTRIBUTIONS_ALL.PO_DISTRIBUTION_ID = PO_DISTRIBUTIONS_ALL.PO_DISTRIBUTION_ID
AND PO_DISTRIBUTIONS_ALL.LAST_UPDATE_DATE > TO_DATE (:P_LAST_EXTRACT_DATE,'MM/D D/YYYY HH24:MI:SS')
UNION
SELECT DISTINCT AP_INVOICE_DISTRIBUTIONS_ALL.INVOICE_ID
FROM PO_DISTRIBUTIONS_ALL
,AP_INVOICE_DISTRIBUTIONS_ALL
,PO_HEADERS_ALL
WHERE AP_INVOICE_DISTRIBUTIONS_ALL.PO_DISTRIBUTION_ID = PO_DISTRIBUTIONS_ALL.PO_DISTRIBUTION_ID
AND PO_DISTRIBUTIONS_ALL.PO_HEADER_ID = PO_HEADERS_ALL.PO_HEADER_ID
UNION
SELECT DISTINCT AP_INVOICE_DISTRIBUTIONS_ALL.INVOICE_ID
FROM PO_DISTRIBUTIONS_ALL, AP_INVOICE_DISTRIBUTIONS_ALL, PO_LINES_ALL
WHERE AP_INVOICE_DISTRIBUTIONS_ALL.PO_DISTRIBUTION_ID = PO_DISTRIBUTIONS_ALL.PO_DISTRIBUTION_ID
AND PO_DISTRIBUTIONS_ALL.PO_LINE_ID = PO_LINES_ALL.PO_LINE_ID
AND PO_LINES_ALL.LAST_UPDATE_DATE > TO_DATE (:P_LAST_EXTRACT_DATE,'MM/DD/YYYY H H24:MI:SS')
) TEMP
WHERE AP_INVOICE_DISTRIBUTIONS_ALL.INVOICE_ID = AP_INVOICES_ALL.INVOICE_ID
AND AP_INVOICES_ALL.VENDOR_ID = PO_VENDORS.VENDOR_ID(+)
AND AP_INVOICES_ALL.VENDOR_SITE_ID = PO_VENDOR_SITES_ALL.VENDOR_SITE_ID(+)
AND AP_INVOICE_DISTRIBUTIONS_ALL.PO_DISTRIBUTION_ID = PO_DISTRIBUTIONS_ALL.PO_DISTRIBUTION_ID(+)
AND PO_DISTRIBUTIONS_ALL.PO_HEADER_ID = PO_HEADERS_ALL.PO_HEADER_ID(+)
AND PO_DISTRIBUTIONS_ALL.PO_HEADER_ID = PO_LINES_ALL.PO_HEADER_ID(+)
AND PO_DISTRIBUTIONS_ALL.PO_LINE_ID = PO_LINES_ALL.PO_LINE_ID(+)
AND AP_INVOICE_DISTRIBUTIONS_ALL.INVOICE_ID = TEMP.INVOICE_ID
AND AP_INVOICE_DISTRIBUTIONS_ALL.INVOICE_ID = AP_INVOICE_LINES_ALL.INVOICE_ID
AND AP_INVOICE_DISTRIBUTIONS_ALL.INVOICE_LINE_NUMBER = AP_INVOICE_LINES_ALL.LINE_NUMBER