Wednesday, 18 March 2026

Query to find Bill To, Ship To Customer and Amount for the sales orders

 SELECT 
    hou.name,
    ooha.org_id,
    ooha.order_number,
    (
        SELECT
            okhb.contract_number
            || '-'
            || okhb.contract_number_modifier
        FROM
            okc_k_headers_all_b  okhb,
            okc_k_rel_objs       rel
        WHERE
                1 = 1
            AND rel.chr_id = okhb.id
            AND rel.jtot_object1_code = 'OKX_ORDERHEAD'
            AND rel.object1_id1 = ooha.header_id
            and rownum <=1
    )                                                                                  service_contract_number,
    ooha.blanket_number,
    ott.name                                                                           order_type,
    bill_hp.party_name                                                                 customer_name,
    bill_hca.account_number                                                            customer_account_number,
    bill_hps.party_site_number                                                         bill_party_site_number,
    bill_hl.address1                                                                   bill_address1,
    bill_hl.address2                                                                   bill_address2,
    bill_hl.city                                                                       bill_city,
    bill_hl.state                                                                      bill_state,
    bill_hl.postal_code                                                                bill_postal_code,
    bill_hl.country                                                                    bill_country,
    ship_hps.party_site_number                                                         ship_party_site_number,
    ship_hl.address1                                                                   ship_address1,
    ship_hl.address2                                                                   ship_address2,
    ship_hl.city                                                                       ship_city,
    ship_hl.state                                                                      ship_state,
    ship_hl.postal_code                                                                ship_postal_code,
    ship_hl.country                                                                    ship_country,
    ooha.transactional_curr_code,
    nvl(oe_totals_grp.get_order_total(ooha.header_id, NULL, 'LINES'), 0)               order_subtotal,
    nvl(oe_totals_grp.get_order_total(ooha.header_id, NULL, 'TAXES'), 0)               order_tax,
    nvl(oe_totals_grp.get_order_total(ooha.header_id, NULL, 'ALL'), 0)                 order_total    
FROM
    oe_order_headers_all     ooha,
  --  oe_order_lines_all       oola,
    oe_transaction_types_tl  ott,
    hz_cust_site_uses_all    bill_hcsua,
    hz_cust_acct_sites_all   bill_hcasa,
    hz_cust_accounts         bill_hca,
    hz_party_sites           bill_hps,
    hz_parties               bill_hp,
    hz_locations             bill_hl,
    hz_cust_site_uses_all    ship_hcsua,
    hz_cust_acct_sites_all   ship_hcasa,
    hz_cust_accounts         ship_hca,
    hz_party_sites           ship_hps,
    hz_parties               ship_hp,
    hz_locations             ship_hl,
    hr_operating_units       hou
WHERE
        1 = 1
   -- AND ooha.header_id = oola.header_id
   -- AND ooha.org_id = oola.org_id
    AND ooha.order_type_id = ott.transaction_type_id
    AND ooha.org_id = hou.organization_id
    AND ooha.invoice_to_org_id = bill_hcsua.site_use_id
    AND bill_hcsua.cust_acct_site_id = bill_hcasa.cust_acct_site_id
    AND bill_hcasa.cust_account_id = bill_hca.cust_account_id
    AND bill_hcasa.party_site_id = bill_hps.party_site_id
    AND bill_hca.party_id = bill_hp.party_id
    AND bill_hps.location_id = bill_hl.location_id
    AND ooha.ship_to_org_id = ship_hcsua.site_use_id
    AND ship_hcsua.cust_acct_site_id = ship_hcasa.cust_acct_site_id
    AND ship_hcasa.cust_account_id = ship_hca.cust_account_id
    AND ship_hcasa.party_site_id = ship_hps.party_site_id
    AND ship_hca.party_id = ship_hp.party_id
    AND ship_hps.location_id = ship_hl.location_id
    --    AND oola.tax_value > 0
    --AND ooha.order_number = '111111111'
    --AND nvl(oe_totals_grp.get_order_total(ooha.header_id, NULL, 'TAXES'), 0) > 0
    AND trunc(ooha.booked_date) >= '01-JAN-2026';

No comments:

Post a Comment