Wednesday 25 July 2012

How to use Union All in ORACLE



select
           *
from

(

(select
       distinct
 --      i.prepayments_applied_flag,
--       i.prepay_flag,
--       i.prepaid_amount,
       i.vendor_name,
       i.vendor_site_code,
       i.vendor_number,
       i.invoice_type,
       i.invoice_date,
       i.invoice_num,
       i.doc_sequence_value vno,
       c.check_number,

       i.invoice_currency_code,
       i.invoice_amount,
       i.prepaid_amount,
       i.gl_date,
       c.exchange_date payment_rate_date,
       c.exchange_rate,
       c.exchange_rate_type,
       i.description
/*       (select
             abs(nvl(SUM(ael.entered_dr)-SUM(ael.entered_cr),0) )

        from
             ap_ae_lines_all ael,
             ap_ae_headers_all aeh,
             ap_invoices_all api
             ,po_vendors po

        where     aeh.ae_header_id              =     ael.ae_header_id
        and         api.invoice_id                    =      ael.reference2
        and         po.vendor_name               =       ael.reference1
        AND      ael.reference10  NOT IN ('CHARGE' , 'AWT')

        and      aeh.accounting_date <:From_date
        and                po.vendor_id=200304
        and api.set_of_books_id=1003
        and api.org_id=302
        GROUP BY  po.vendor_name,po.vendor_id
       )Opening_bal

*/
     
     
     
     
     
     
     
   
from
     ap_invoices_v i ,
     ap_checks_v c,
     ap_invoice_payments_v p


where i.invoice_id=p.INVOICE_ID(+)
and c.check_id(+)=p.CHECK_ID
and c.check_status(+)<> 'Voided'
and i.posting_flag='Y'                                                              ------this flag shows that this invoice has been accounted or not --------
--and i.invoice_num='JV-1-236'--JV-3-4'
--and i.gl_date ='06-jan-09' --06-jan-09'

--and   i.gl_date between '01-jul-08' and '30-jun-09'
and   i.gl_date between :From_Date and :To_date

--and i.vendor_name = nvl(:Sup_name,i.vendor_name)
--and i.payment_status=nvl(:inv_paid_stat,i.payment_status)
--and i.vendor_id=200304
and  to_number(i.vendor_number) between nvl(:Vendor_From,i.vendor_number) and nvl(:Vendor_To,i.vendor_number)
and i.approval_status_lookup_code<>'CANCELLED'
and i.invoice_type in ('Prepayment')
--and i.prepayments_applied_flag='Y'
and i.payment_status_flag='Y'
)

union all


(
select
       distinct
--       i.prepayments_applied_flag,
--       i.prepay_flag,
--       i.prepaid_amount,
       i.vendor_name,
       i.vendor_site_code,
       i.vendor_number,
       i.invoice_type,
       i.invoice_date,
       i.invoice_num,
       i.doc_sequence_value vno,
       c.check_number,

       i.invoice_currency_code,
       i.invoice_amount,
       i.prepaid_amount,
       i.gl_date,
       c.exchange_date payment_rate_date,
       c.exchange_rate,
       c.exchange_rate_type,
       i.description

/*
       (select
 
           abs(nvl(SUM(ael.entered_dr)-SUM(ael.entered_cr),0) )

        from
             ap_ae_lines_all ael,
             ap_ae_headers_all aeh,
             ap_invoices_all api
             ,po_vendors po

        where     aeh.ae_header_id              =     ael.ae_header_id
        and         api.invoice_id                    =      ael.reference2
        and         po.vendor_name               =       ael.reference1
        AND      ael.reference10  NOT IN ('CHARGE' , 'AWT')

        and      aeh.accounting_date <:From_date
        and                po.vendor_id=200304
        and api.set_of_books_id=1003
        and api.org_id=302
        GROUP BY  po.vendor_name,po.vendor_id
       )Opening_bal

*/  

from
     ap_invoices_v i ,
     ap_checks_v c,
     ap_invoice_payments_v p


where i.invoice_id=p.INVOICE_ID(+)
and c.check_id(+)=p.CHECK_ID
and c.check_status(+)<> 'Voided'
and i.posting_flag='Y'                                                              ------this flag shows that this invoice has been accounted or not --------
--and   i.gl_date between '01-jul-08' and '30-jun-09'
and   i.gl_date between :From_Date and :To_date
--and i.vendor_id=200304
and  to_number(i.vendor_number) between nvl(:Vendor_From,i.vendor_number) and nvl(:Vendor_To,i.vendor_number)
and i.approval_status_lookup_code<>'CANCELLED'
and i.invoice_type in ('Standard')
and i.prepayments_applied_flag='Y'
and i.payment_status_flag in ('Y', 'P' )   --' P for partial '
--order by gl_date
)

) a
order by a.invoice_type,a.gl_date,a.invoice_date

No comments:

Post a Comment