AP Trail Balances SQL Query for R12
SELECT aia.invoice_num, aia.description inv_description,aia.attribute2 "Dept", aps.segment1 vendor#,
aps.vendor_name, aia.invoice_currency_code, aia.invoice_amount,tb.diff "Amount Remain",
gcc.segment1
|| '.'
|| gcc.segment2
|| '.'
|| gcc.segment3
|| '.'
|| gcc.segment4
|| '.'
|| gcc.segment5
|| '.'
|| gcc.segment6
|| '.'
|| gcc.segment7 ACCOUNT,
aia.invoice_type_lookup_code,
aia.invoice_date, aia.attribute5 status,
ap.NAME terms
FROM xla.xla_transaction_entities xte,
(SELECT tb.code_combination_id,
NVL (tb.applied_to_entity_id, tb.source_entity_id) entity_id,
SUM (NVL (tb.acctd_rounded_cr, 0)),
SUM (NVL (tb.acctd_rounded_dr, 0)),
SUM (NVL (tb.acctd_rounded_cr, 0))
- SUM (NVL (tb.acctd_rounded_dr, 0)) diff,
party_id
FROM xla_trial_balances tb
WHERE tb.definition_code =:definition_code -- Ex : 'AP_200_1001'
and trunc(tb.gl_date) <=TO_DATE ('1-NOV-2011')
GROUP BY tb.code_combination_id,
NVL (tb.applied_to_entity_id, tb.source_entity_id),
tb.party_id
HAVING SUM (NVL (tb.acctd_rounded_cr, 0)) <>
SUM (NVL (tb.acctd_rounded_dr, 0))) tb,
ap_invoices_all aia,
ap_suppliers aps,
gl_code_combinations gcc,
ap_terms ap
WHERE tb.entity_id = xte.entity_id
AND xte.application_id =:application_id --Ex : 200
AND xte.source_id_int_1 = aia.invoice_id
AND aia.vendor_id = aps.vendor_id
AND tb.code_combination_id = gcc.code_combination_id
AND ap.term_id = aia.terms_id
-----------------------
aps.vendor_name, aia.invoice_currency_code, aia.invoice_amount,tb.diff "Amount Remain",
gcc.segment1
|| '.'
|| gcc.segment2
|| '.'
|| gcc.segment3
|| '.'
|| gcc.segment4
|| '.'
|| gcc.segment5
|| '.'
|| gcc.segment6
|| '.'
|| gcc.segment7 ACCOUNT,
aia.invoice_type_lookup_code,
aia.invoice_date, aia.attribute5 status,
ap.NAME terms
FROM xla.xla_transaction_entities xte,
(SELECT tb.code_combination_id,
NVL (tb.applied_to_entity_id, tb.source_entity_id) entity_id,
SUM (NVL (tb.acctd_rounded_cr, 0)),
SUM (NVL (tb.acctd_rounded_dr, 0)),
SUM (NVL (tb.acctd_rounded_cr, 0))
- SUM (NVL (tb.acctd_rounded_dr, 0)) diff,
party_id
FROM xla_trial_balances tb
WHERE tb.definition_code =:definition_code -- Ex : 'AP_200_1001'
and trunc(tb.gl_date) <=TO_DATE ('1-NOV-2011')
GROUP BY tb.code_combination_id,
NVL (tb.applied_to_entity_id, tb.source_entity_id),
tb.party_id
HAVING SUM (NVL (tb.acctd_rounded_cr, 0)) <>
SUM (NVL (tb.acctd_rounded_dr, 0))) tb,
ap_invoices_all aia,
ap_suppliers aps,
gl_code_combinations gcc,
ap_terms ap
WHERE tb.entity_id = xte.entity_id
AND xte.application_id =:application_id --Ex : 200
AND xte.source_id_int_1 = aia.invoice_id
AND aia.vendor_id = aps.vendor_id
AND tb.code_combination_id = gcc.code_combination_id
AND ap.term_id = aia.terms_id
-----------------------
AP Invoice liability (Trial Balance) Query: R12 AP GL PO RCV XLA
SELECT NVL (f.currency_code, d.invoice_currency_code) currency_code,
d.invoice_amount, d.wfapproval_status, d.invoice_num, d.invoice_id,
i.segment1 v_code, i.vendor_name,
NVL (d.invoice_amount, 0) - NVL (d.amount_paid, 0) amt,
SUM (a.acctd_rounded_cr) - SUM (a.acctd_rounded_dr) amount,
b.accounting_date, f.segment1 po_num, f.comments descr,
h.receipt_num rct_num, c.segment6 loc, c.segment2 cost_centre,
d.doc_sequence_value ap_voucher, j.employee_number, j.full_name
FROM xla_transaction_entities xte,
ap.ap_invoice_distributions_all b,
ap.ap_invoice_lines_all k,
gl.gl_code_combinations c,
ap.ap_invoices_all d,
po.po_distributions_all e,
po.po_headers_all f,
apps.rcv_transactions g,
apps.rcv_shipment_headers h,
apps.po_vendors i,
apps.per_all_people_f j,
(SELECT /*+ index(tb XLA_TRIAL_BALANCES_N2) */
tb.code_combination_id, tb.ledger_id, tb.party_id party_id,
tb.gl_date, tb.ae_header_id,
NVL (tb.applied_to_entity_id,
tb.source_entity_id
) entity_id,
tb.definition_code, tb.record_type_code,
SUM (NVL (tb.acctd_rounded_cr, 0)) acctd_rounded_cr,
SUM (NVL (tb.acctd_rounded_dr, 0)) acctd_rounded_dr,
SUM (NVL (tb.acctd_rounded_cr, 0))
- SUM (NVL (tb.acctd_rounded_dr, 0)) diff,
party_id
FROM xla_trial_balances tb
WHERE tb.definition_code = 'AP_200_1001'
AND tb.gl_date <= :acc_date --GL Date Value
AND tb.code_combination_id IN (SELECT code_combination_id
FROM gl_code_combinations a
WHERE a.segment3 = :acc_code)--GL CC Value
GROUP BY tb.code_combination_id,
tb.ledger_id,
tb.party_id,
tb.party_id,
NVL(tb.applied_to_entity_id,tb.source_entity_id),
tb.record_type_code,
tb.party_id,
tb.gl_date,
tb.definition_code,
tb.ae_header_id) a
WHERE a.code_combination_id = c.code_combination_id
AND d.INVOICE_NUM = :invoice_num --Invoice Num
AND a.definition_code = 'AP_200_1001'
AND a.entity_id = xte.entity_id
AND xte.application_id = 200
AND xte.ledger_id = a.ledger_id
AND NVL (source_id_int_1, (-99)) = d.invoice_id
AND d.invoice_id = k.invoice_id
AND k.line_number = b.invoice_line_number
AND c.segment3 = :acc_code --GL CC Code
AND a.gl_date <= :acc_date --Gl Date
AND k.invoice_id = b.invoice_id
AND b.distribution_line_number = '1'
AND k.line_number = '1'
AND b.po_distribution_id = e.po_distribution_id(+)
AND e.po_header_id = f.po_header_id(+)
AND b.rcv_transaction_id = g.transaction_id(+)
AND g.shipment_header_id = h.shipment_header_id(+)
AND f.agent_id = j.person_id(+)
AND i.vendor_id = d.vendor_id
AND d.set_of_books_id = a.ledger_id
AND d.org_id = :l_org_id --Invoice Org Id
AND TRUNC (SYSDATE) BETWEEN j.effective_start_date(+)
AND j.effective_end_date(+)
HAVING SUM (a.acctd_rounded_cr) - SUM (a.acctd_rounded_dr) <> 0
GROUP BY f.currency_code,
d.invoice_amount,
b.project_id,
d.wfapproval_status,
d.invoice_num,
d.invoice_id,
b.accounting_date,
f.segment1,
h.receipt_num,
i.segment1,
i.vendor_name,
c.segment6,
d.doc_sequence_value,
c.segment2,
j.employee_number,
j.full_name,
f.agent_id,
f.comments,
d.invoice_currency_code,
b.posted_flag,
d.amount_paid,
d.invoice_id
d.invoice_amount, d.wfapproval_status, d.invoice_num, d.invoice_id,
i.segment1 v_code, i.vendor_name,
NVL (d.invoice_amount, 0) - NVL (d.amount_paid, 0) amt,
SUM (a.acctd_rounded_cr) - SUM (a.acctd_rounded_dr) amount,
b.accounting_date, f.segment1 po_num, f.comments descr,
h.receipt_num rct_num, c.segment6 loc, c.segment2 cost_centre,
d.doc_sequence_value ap_voucher, j.employee_number, j.full_name
FROM xla_transaction_entities xte,
ap.ap_invoice_distributions_all b,
ap.ap_invoice_lines_all k,
gl.gl_code_combinations c,
ap.ap_invoices_all d,
po.po_distributions_all e,
po.po_headers_all f,
apps.rcv_transactions g,
apps.rcv_shipment_headers h,
apps.po_vendors i,
apps.per_all_people_f j,
(SELECT /*+ index(tb XLA_TRIAL_BALANCES_N2) */
tb.code_combination_id, tb.ledger_id, tb.party_id party_id,
tb.gl_date, tb.ae_header_id,
NVL (tb.applied_to_entity_id,
tb.source_entity_id
) entity_id,
tb.definition_code, tb.record_type_code,
SUM (NVL (tb.acctd_rounded_cr, 0)) acctd_rounded_cr,
SUM (NVL (tb.acctd_rounded_dr, 0)) acctd_rounded_dr,
SUM (NVL (tb.acctd_rounded_cr, 0))
- SUM (NVL (tb.acctd_rounded_dr, 0)) diff,
party_id
FROM xla_trial_balances tb
WHERE tb.definition_code = 'AP_200_1001'
AND tb.gl_date <= :acc_date --GL Date Value
AND tb.code_combination_id IN (SELECT code_combination_id
FROM gl_code_combinations a
WHERE a.segment3 = :acc_code)--GL CC Value
GROUP BY tb.code_combination_id,
tb.ledger_id,
tb.party_id,
tb.party_id,
NVL(tb.applied_to_entity_id,tb.source_entity_id),
tb.record_type_code,
tb.party_id,
tb.gl_date,
tb.definition_code,
tb.ae_header_id) a
WHERE a.code_combination_id = c.code_combination_id
AND d.INVOICE_NUM = :invoice_num --Invoice Num
AND a.definition_code = 'AP_200_1001'
AND a.entity_id = xte.entity_id
AND xte.application_id = 200
AND xte.ledger_id = a.ledger_id
AND NVL (source_id_int_1, (-99)) = d.invoice_id
AND d.invoice_id = k.invoice_id
AND k.line_number = b.invoice_line_number
AND c.segment3 = :acc_code --GL CC Code
AND a.gl_date <= :acc_date --Gl Date
AND k.invoice_id = b.invoice_id
AND b.distribution_line_number = '1'
AND k.line_number = '1'
AND b.po_distribution_id = e.po_distribution_id(+)
AND e.po_header_id = f.po_header_id(+)
AND b.rcv_transaction_id = g.transaction_id(+)
AND g.shipment_header_id = h.shipment_header_id(+)
AND f.agent_id = j.person_id(+)
AND i.vendor_id = d.vendor_id
AND d.set_of_books_id = a.ledger_id
AND d.org_id = :l_org_id --Invoice Org Id
AND TRUNC (SYSDATE) BETWEEN j.effective_start_date(+)
AND j.effective_end_date(+)
HAVING SUM (a.acctd_rounded_cr) - SUM (a.acctd_rounded_dr) <> 0
GROUP BY f.currency_code,
d.invoice_amount,
b.project_id,
d.wfapproval_status,
d.invoice_num,
d.invoice_id,
b.accounting_date,
f.segment1,
h.receipt_num,
i.segment1,
i.vendor_name,
c.segment6,
d.doc_sequence_value,
c.segment2,
j.employee_number,
j.full_name,
f.agent_id,
f.comments,
d.invoice_currency_code,
b.posted_flag,
d.amount_paid,
d.invoice_id
Hi Nasiruddin
ReplyDeleteI am having a report Supplier due for inactivation and attached is the sql .I need to add Ap Current Balance column to the report . Is this possible , Can I add it to the report using the existing tables in the query .
SSELECT
ReplyDeleteTable__46.COMPANY_NAME,
Table__46.ORG_ID,
Table__46.PAY_GROUP_LOOKUP_CODE,
Table__46.VENDOR_ID,
Table__46.VENDOR_NAME,
Table__46.VENDOR_NUMBER,
Table__46.VENDOR_CREATION_DATE,
Table__46.VENDOR_SITE_ID,
Table__46.VENDOR_SITE_CODE,
Table__46.SITE_CREATION_DATE,
Table__46.LAST_AP_DOC
FROM
(
SELECT hou.name company_name, vs.org_id, vs.pay_group_lookup_code, v.vendor_id, v.vendor_name, v.segment1 vendor_number, v.creation_date vendor_creation_date, vs.vendor_site_id, vs.vendor_site_code
, vs.creation_date site_creation_date,
GREATEST((select MAX(api.invoice_date)
from ap_invoices_all api
, ap_supplier_sites_all s
where api.org_id = vs.org_id
and s.vendor_site_id = api.vendor_site_id
and s.inactive_date is null
--and api.invoice_date >= trunc(add_months(sysdate,-6))
and s.vendor_site_id = vs.vendor_site_id),
(select MAX(aca.check_date)
from ap_checks_all aca
, ap_supplier_sites_all s
where aca.status_lookup_code = 'NEGOTIABLE'
and aca.org_id = vs.org_id
and s.vendor_site_id = aca.vendor_site_id
and s.inactive_date is null
--and aca.check_date >= trunc(add_months(sysdate,-6))
and aca.vendor_site_id = vs.vendor_site_id)) LAST_AP_DOC
from apps.hr_all_organization_units hou
, apps.ap_supplier_sites_all vs
, apps.ap_suppliers v
where v.vendor_id = vs.vendor_id
and NVL(v.end_date_active, sysdate+1) > sysdate
AND NVL(v.start_date_active, sysdate-1) < sysdate
and vs.inactive_date is null
and trunc(vs.creation_date) < add_months(trunc(sysdate),-3)
and hou.organization_id = vs.org_id
and not exists
ReplyDelete(select api.invoice_date
from apps.ap_invoices_all api
, apps.ap_supplier_sites_all s
, apps.ap_payment_schedules_all aps
where api.org_id = vs.org_id
and api.invoice_id = aps.invoice_id
and s.vendor_site_id = api.vendor_site_id
and s.inactive_date is null
and (trunc(api.invoice_date) >= trunc(add_months(sysdate,-12))
or aps.amount_remaining <> 0)
and s.vendor_site_id = vs.vendor_site_id
union
select aca.check_date
from apps.ap_checks_all aca
, apps.ap_supplier_sites_all s
where aca.status_lookup_code = 'NEGOTIABLE'
and aca.org_id = vs.org_id
and s.vendor_site_id = aca.vendor_site_id
and s.inactive_date is null
and trunc(aca.check_date) >= trunc(add_months(sysdate,-12))
and aca.vendor_site_id = vs.vendor_site_id)
and vs.vendor_site_code||vs.org_id not in
(
select hcu.location||hcu.org_id
ReplyDeletefrom apps.ar_payment_schedules_all ps
, apps.hz_cust_accounts hca
, apps.hz_cust_acct_sites_all hcs
, apps.hz_cust_site_uses_all hcu
, apps.hz_parties hp
, apps.hz_party_sites hps
, apps.hz_locations hloc
, apps.ar_receivable_applications_all arapp
where arapp.org_id = vs.org_id --KB TO M= add_months(trunc(sysdate),-12)
UNION
select hcu.location||hcu.org_id
from apps.ar_payment_schedules_all ps
, apps.ra_customer_trx_all rct
, apps.hz_cust_accounts hca
, apps.hz_cust_acct_sites_all hcs
, apps.hz_cust_site_uses_all hcu
, apps.hz_parties hp
, apps.hz_party_sites hps
, apps.hz_locations hloc
, apps.ra_cust_trx_types_all rctt
, apps.ra_cust_trx_line_gl_dist_all gld
, apps.gl_code_combinations cc
where gld.org_id = vs.org_id
AND rctt.org_id = ps.org_id
ReplyDeleteand rct.customer_trx_id = ps.customer_trx_id
and ps.customer_id = hca.cust_account_id
and ps.customer_site_use_id = hcu.site_use_id
and hca.cust_account_id = hcs.cust_account_id
and hcs.cust_acct_site_id = hcu.cust_acct_site_id
and hca.party_id = hp.party_id
and hp.party_id = hps.party_id(+)
and hps.location_id = hloc.location_id(+)
and hcs.party_site_id = hps.party_site_id
and rctt.cust_trx_type_id = ps.cust_trx_type_id
and ps.customer_trx_id = gld.customer_trx_id
and gld.account_class = 'REC'
and gld.latest_rec_flag = 'Y'
and gld.code_combination_id = cc.code_combination_id
and trunc(ps.trx_date) >= add_months(trunc(sysdate),-12)
)
ORDER BY VENDOR_NAME ASC , VENDOR_SITE_CODE ASC
) Table__46
WHERE
(
(
Table__46.LAST_AP_DOC Is Null
OR
Table__46.LAST_AP_DOC <= @prompt('Date of Last Transaction','D','Inactive Suppliers\Last Ap Doc',Mono,Free,Not_Persistent,,User:0)
)
AND
Table__46.SITE_CREATION_DATE <= @prompt('Date of Last Transaction','D','Inactive Suppliers\Site Creation Date',Mono,Free,Not_Persistent,,User:1)
AND
Table__46.COMPANY_NAME IN @prompt('Enter value(s) for Company Name','A','Inactive Suppliers\Company Name',Multi,Free,Persistent,,User:2,optional)
)