Saturday, January 18, 2020

sale order back to back query


SELECT   hou.NAME ORGANIZATION, hp_ship.party_name customer,
         oh.cust_po_number customer_number, hca.account_number,
         oh.order_number so_no, ot.NAME order_type, oh.ordered_date so_date,
         oh.flow_status_code status, msib.segment1 item, msib.description,
         ordered_quantity, ship_to.LOCATION ship_to_location,
         ship_loc.address1 ship_to_address1,
         ship_loc.address2 ship_to_address2,
         ship_loc.address3 ship_to_address3,
         ship_loc.address4 ship_to_address4,
            DECODE (ship_loc.city,
                    NULL, NULL,
                    ship_loc.city || ', '
                   )
         || DECODE (ship_loc.state,
                    NULL, ship_loc.province || ', ',
                    ship_loc.state || ', '
                   )
         || DECODE (ship_loc.postal_code,
                    NULL, NULL,
                    ship_loc.postal_code || ', '
                   )
         || DECODE (ship_loc.country, NULL, NULL, ship_loc.country)
                                                             ship_to_address5,
         ship_to.LOCATION bill_to_location,
         bill_loc.address1 bill_to_address1,
         bill_loc.address2 bill_to_address2,
         bill_loc.address3 bill_to_address3,
         bill_loc.address4 bill_to_address4,
            DECODE (bill_loc.city,
                    NULL, NULL,
                    bill_loc.city || ', '
                   )
         || DECODE (bill_loc.state,
                    NULL, bill_loc.province || ', ',
                    bill_loc.state || ', '
                   )
         || DECODE (bill_loc.postal_code,
                    NULL, NULL,
                    bill_loc.postal_code || ', '
                   )
         || DECODE (bill_loc.country, NULL, NULL, bill_loc.country)
                                                             bill_to_address5,
         bill_to.LOCATION bill_to_location,
         ship_loc.address1 bill_to_address1,
         ship_loc.address2 bill_to_address2,
         ship_loc.address3 bill_to_address3,
         ship_loc.address4 bill_to_address4,
            DECODE (ship_loc.city,
                    NULL, NULL,
                    ship_loc.city || ', '
                   )
         || DECODE (ship_loc.state,
                    NULL, ship_loc.province || ', ',
                    ship_loc.state || ', '
                   )
         || DECODE (ship_loc.postal_code,
                    NULL, NULL,
                    ship_loc.postal_code || ', '
                   )
         || DECODE (ship_loc.country, NULL, NULL, ship_loc.country)
                                                             bill_to_address5,
         bill_to.LOCATION bill_to_location,
         bill_loc.address1 bill_to_address1,
         bill_loc.address2 bill_to_address2,
         bill_loc.address3 bill_to_address3,
         bill_loc.address4 bill_to_address4,
            DECODE (bill_loc.city,
                    NULL, NULL,
                    bill_loc.city || ', '
                   )
         || DECODE (bill_loc.state,
                    NULL, bill_loc.province || ', ',
                    bill_loc.state || ', '
                   )
         || DECODE (bill_loc.postal_code,
                    NULL, NULL,
                    bill_loc.postal_code || ', '
                   )
         || DECODE (bill_loc.country, NULL, NULL, bill_loc.country)
                                                             bill_to_address5
    FROM apps.oe_order_headers_all oh,
         apps.oe_order_lines_all ol,
         apps.hr_operating_units hou,
         apps.oe_transaction_types_tl ot,
         apps.mtl_system_items_b msib,
         apps.hz_cust_accounts_all hca,                                      --
         apps.hz_parties hp_ship,
         apps.hz_cust_site_uses_all ship_to,
         apps.hz_cust_acct_sites_all ship_cu,
         apps.hz_party_sites ship_ps,
         apps.hz_locations ship_loc,
         apps.hz_parties hp_bill,
         apps.hz_cust_site_uses_all bill_to,
         apps.hz_cust_acct_sites_all bill_cu,
         apps.hz_party_sites bill_ps,
         apps.hz_locations bill_loc
   WHERE 1 = 1
     AND source_type_code = 'EXTERNAL'
     AND oh.header_id = ol.header_id
     AND hou.organization_id = oh.org_id
     AND oh.sold_to_org_id = hca.cust_account_id
     AND hca.party_id = hp_ship.party_id
     AND ship_to.site_use_id = oh.ship_to_org_id
     AND ship_to.site_use_code = 'SHIP_TO'
     AND ship_to.cust_acct_site_id = ship_cu.cust_acct_site_id
     AND ship_ps.party_site_id = ship_cu.party_site_id
     AND ship_loc.location_id = ship_ps.location_id
     AND ot.transaction_type_id = oh.order_type_id
     AND ol.ordered_item = msib.segment1
     AND oh.ship_from_org_id = msib.organization_id
     -------
     AND hca.party_id = hp_bill.party_id
     AND bill_to.site_use_id = oh.invoice_to_org_id
     AND bill_to.site_use_code = 'BILL_TO'
     AND bill_to.cust_acct_site_id = bill_cu.cust_acct_site_id
     AND bill_ps.party_site_id = bill_cu.party_site_id
     AND bill_loc.location_id = bill_ps.location_id
ORDER BY oh.ordered_date DESC

No comments:

Post a Comment