Monday, 21 January 2013

Supplier wise 5 Buckets Aging Query In AP

select yy.VENDOR_NAME,yy.LESS30,yy.GREATER30,yy.GREATER60,yy.GREATER90,yy.GREATER120,
(select count(1)
from ap_invoices_all aia
where aia.CANCELLED_DATE is null
and aia.VENDOR_ID=yy.vendor_id
and trunc(aia.INVOICE_DATE) between :p_from_date and  :p_to_date
and aia.AMOUNT_PAID <> aia.INVOICE_AMOUNT ) OUT_STND_BILL,
(select sum(nvl(apd.PREPAY_AMOUNT_REMAINING,0))
from ap_invoices_all aib,ap_invoice_distributions_all apd
where aib.CANCELLED_DATE is null
and aib.VENDOR_ID=yy.vendor_id
and aib.INVOICE_ID=apd.INVOICE_ID
and apd.REVERSAL_FLAG='N'
and trunc(aib.INVOICE_DATE) between :p_from_date and  :p_to_date
and aib.INVOICE_TYPE_LOOKUP_CODE='PREPAYMENT'
) ON_AC_AMOUNT
from
(select
xx.VENDOR_NAME,xx.VENDOR_ID,sum(nvl(xx.less_30,0)) LESS30,sum(nvl(xx.greater_30,0)) GREATER30,sum(nvl(xx.greater_60,0)) GREATER60,
sum(nvl(xx.greater_90,0)) GREATER90,sum(nvl(xx.greater_120,0)) GREATER120
from
(select c.VENDOR_NAME,c.VENDOR_ID,
(case when((sysdate) - trunc(a.DUE_DATE) <= 30) then nvl(a.AMOUNT_REMAINING,0)
else 0 end ) less_30,
(case when((sysdate) - trunc(a.DUE_DATE) between 31 and 60) then nvl(a.AMOUNT_REMAINING,0)
else 0 end ) greater_30,
(case when((sysdate) - trunc(a.DUE_DATE) between 61 and 90) then nvl(a.AMOUNT_REMAINING,0)
else 0 end ) greater_60,
(case when((sysdate) - trunc(a.DUE_DATE) between 91 and 120) then nvl(a.AMOUNT_REMAINING,0)
else 0 end ) greater_90,
(case when((sysdate) - trunc(a.DUE_DATE) > 120) then nvl(a.AMOUNT_REMAINING,0)
else 0 end ) greater_120
from ap_payment_schedules_all a,
ap_invoices_all b,
po_vendors c
where a.INVOICE_ID=b.INVOICE_ID
and b.VENDOR_ID=c.VENDOR_ID
and trunc(b.INVOICE_DATE) between :p_from_date and :p_to_date
and b.CANCELLED_DATE is null
order by c.VENDOR_NAME ) xx
group by xx.VENDOR_NAME,xx.VENDOR_ID) yy
order by yy.vendor_name ;

2 comments:

  1. Sourav.. Excellant work..

    Can you put a condition to exclude employees? I ran this query and my list includes employees as well..

    2 things-
    1 - exclude supplier, where supplier type = employee
    2 - Total coloumn? ( on bottom and on right)?

    ReplyDelete