SELECT distinct
aia.invoice_id,
aia.invoice_num,
aia.invoice_date,
aia.invoice_amount,
aia.invoice_currency_code,
aia.payment_currency_code,
aia.gl_date,
xah.period_name,
aia.payment_method_code,
xah.je_category_name,
xev.event_id,
xte.ENTITY_CODE
,xte.APPLICATION_ID
,xte.legal_entity_id
,source_id_int_1
,source_application_id
,source_id_int_2
,source_id_char_1
,gjh.status
FROM
ap.ap_invoices_all aia,
xla.xla_transaction_entities xte,
xla.xla_events xev,
xla.xla_ae_headers xah,
xla.xla_ae_lines xal,
gl_import_references gir,
gl_je_headers gjh,
gl_je_lines gjl,
gl_code_combinations gcc
WHERE
aia.invoice_id = xte.source_id_int_1
AND xev.entity_id = xte.entity_id
AND xah.entity_id = xte.entity_id
AND xah.event_id = xev.event_id
AND xah.ae_header_id = xal.ae_header_id
AND xah.je_category_name = 'XX_CATEGORY_NAME'--Purchase Invoices
AND xah.gl_transfer_status_code = 'Y'
AND xal.gl_sl_link_id = gir.gl_sl_link_id
AND gir.gl_sl_link_table = xal.gl_sl_link_table
AND gjl.je_header_id = gjh.je_header_id
AND gjh.je_header_id = gir.je_header_id
AND gjl.je_header_id = gir.je_header_id
AND gir.je_line_num = gjl.je_line_num
and gjh.name='XX_JOURNAL_NAME'
and aia.invoice_num='XX_INVOICE_NUM';