Outer join operator not allowed with OR in my WHERE clause - Error: ORA-01719 - sql

I need to check for the conditions at the bottom, but I cannot change use OR because of the (+) that are involved. I did not set that part up, so I am not sure how to rearrange it. Can someone tell me what I could do to get those conditions to go through?
SELECT DISTINCT
t.tkt_sid,
ts.tkt_sts_dsc,
tp.prty_dsc,
a.agt_cont_nbr,
au_a.user_nm assigned_to,
t.rcd_lst_user_ts
FROM
afp_asd.tkt t,
afp_asd.tkt_prty tp,
afp_asd.tkt_sts ts,
afp_asd.tkt_type tt,
afp_asd.tkt_log tl,
afp_asd.agt a,
afp_asd.asd_user au_a,
afp_asd.asd_user au_c,
afp_asd.asd_user au_l,
afp_asd.asd_user au_log,
afp_asd.prb_area pa1,
afp_asd.prb_area pa2,
afp_asd.prb_area pa3,
afp_asd.mktg_org mo,
afp_asd.src_sys ss,
afp_asd.agt ofc,
afp_asd.co c,
afp_asd.agt_sts ast
WHERE (
t.prty_cd = tp.prty_cd
AND t.tkt_sts_cd = ts.tkt_sts_cd
AND t.tkt_type_cd = tt.tkt_type_cd
AND t.agt_sid = a.agt_sid
AND t.assigned_id = au_a.asd_user_id
AND t.rcd_crt_user_id = au_c.asd_user_id (+)
AND t.lst_updater_id = au_l.asd_user_id
AND t.tkt_sid = tl.tkt_sid
AND t.prb_area_sid = pa3.prb_area_sid
AND tl.rcd_crt_user_id = au_log.asd_user_id
AND pa3.prb_hier_sid = pa2.prb_area_sid (+)
AND pa2.prb_hier_sid = pa1.prb_area_sid (+)
AND a.mktg_org_cd = mo.mktg_org_cd
AND a.src_sys_cd = mo.src_sys_cd
AND a.src_sys_cd = ss.src_sys_cd
AND a.ofc_id = ofc.agt_cont_nbr (+)
AND a.src_sys_cd = ofc.src_sys_cd (+)
AND a.co_sid = c.co_sid (+)
AND a.agt_sts_cd = ast.agt_sts_cd
AND tl.rcd_lst_user_ts >= :b_start_date
AND t.user_grp_sid = :b_group_id
)
AND (
(TKT_STS_DSC NOT LIKE 'Completed')
OR (
tp.prty_dsc = 'High'
AND ts.tkt_sts_dsc = 'Pending'
AND t.rcd_lst_user_ts < SYSDATE- 2
)
OR (
t.rcd_lst_user_ts < SYSDATE- 3
AND ts.tkt_sts_dsc = 'In Progress'
AND tp.prty_dsc = 'High'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 15
AND ts.tkt_sts_dsc = 'In Progress'
AND tp.prty_dsc = 'Medium'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 28
AND ts.tkt_sts_dsc = 'In Progress'
AND tp.prty_dsc = 'Low'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 7
AND ts.tkt_sts_dsc = 'Pending'
AND tp.prty_dsc = 'High'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 21
AND ts.tkt_sts_dsc = 'Pending'
AND tp.prty_dsc = 'Medium'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 43
AND ts.tkt_sts_dsc = 'Pending'
AND tp.prty_dsc = 'Low'
)
)
ORDER BY ASSIGNED_TO,
PRTY_DSC

Don't use the old FROM TableA,TableB WHERE ... join syntax. Just don't.
Instead, write out your joins individually:
FROM TableA
INNER JOIN TableB ON ...
LEFT JOIN TableC ON ...
This isn't just a general rant against the old syntax: using the new (I say "new", but it's more than 20 years old now), standard syntax will fix your problem in this case.

Related

Nested in line view Add Column

I have to add a column to this report - the new column name is: uda_value_desc
The code below will give me a result if I enter a PM_ITEM number, I need to insert the code into one of the in line views.
Any help would be appreciated
WITH item_temp AS (SELECT im.item
,uv.uda_value_desc uda_value_desc -----
FROM item_master im
,TABLE(mff_report.parse_strings(:PM_item)) t
, uda_item_lov uil
, uda_values uv
WHERE t.Column_Value = uil.item
and uil.UDA_ID = 511
and uv.uda_id = uil.uda_id
and uv.uda_value = uil.uda_value
and im.item = uil.item
UNION ALL
SELECT im.item
,null
FROM item_master im
,TABLE(mff_report.parse_strings(:PM_item)) t
WHERE t.COLUMN_VALUE = im.item_parent
AND im.item_level = im.tran_level
UNION ALL
SELECT sd.item
,null
FROM skulist_detail sd
,TABLE(mff_report.parse_strings(:PM_item_list)) t
WHERE t.COLUMN_VALUE = sd.skulist
UNION ALL
SELECT ia.item
,null
FROM mffecom.item_attr ia
,TABLE(mff_report.parse_strings(:PM_product_id)) t
WHERE t.COLUMN_VALUE = ia.product_id
UNION ALL
SELECT im.item
,null
FROM item_master im
WHERE :PM_item IS NULL
AND :PM_item_list IS NULL
AND :PM_product_id IS NULL )
SELECT v_item.item_parent
,uda_value_desc
,v_item.item
,mff_report.mff_merch_sql.get_brand_name(v_item.item) brand_name
,v_item.item_desc
,v_selling.product_id
,v_product.product_web_desc
,v_product.product_template
,v_product.romance_copy
,v_selling.selling_point_1
,v_selling.selling_point_2
,v_selling.selling_point_3
,v_selling.selling_point_4
,v_selling.selling_point_5
,v_selling.selling_point_6
,v_selling.selling_point_7
,v_selling.selling_point_8
,v_selling.selling_point_9
,v_selling.selling_point_10
,v_selling.selling_point_11
,v_selling.selling_point_12
,v_selling.selling_point_13
,v_selling.selling_point_14
,v_selling.selling_point_15
,v_selling.selling_point_16
,v_item.vpn
,v_item.diff_1
,v_item.diff1_desc
,v_item.diff_2
,v_item.diff2_desc
,v_item.diff_3
,v_item.diff3_desc
,v_item.diff_4
,v_item.diff4_desc
,v_item.supplier
,v_item.sup_name
,v_product.code_desc fulfillment_method_desc
,v_product.air_ship_restrict
,v_product.do_not_freeze
,v_product.free_shipping
,v_product.refrigerate
,v_product.serial_reqd
,v_product.vaccination
,v_product.is_consumable
,v_product.zero_weight
,v_product.restrict_state
,v_product.no_of_alt_image
,v_product.product_status
,v_product.item_ecom_status
,TO_CHAR(v_product.deactivate_date, 'MM/DD/YYYY') product_deactivate_date
,TO_CHAR(v_product.product_activate_date, 'MM/DD/YYYY') product_activate_date
,TO_CHAR(v_product.item_activate_date, 'MM/DD/YYYY') item_activate_date
,TO_CHAR(v_product.item_deactivate_date, 'MM/DD/YYYY') item_deactivate_date
,v_product.prod_created_by
,TO_CHAR(v_product.prod_created_date, 'MM/DD/YYYY') prod_created_date
,v_product.prod_updated_by
,v_product.item_created_by
,TO_CHAR(v_product.item_created_date, 'MM/DD/YYYY') item_created_date
,v_product.item_updated_by
,v_item.delete_type purge
,v_item.dept
,v_item.class
,v_item.subclass
,mff_orders_sql.buyer_for_item(v_item.item) buyer
,mff_orders_sql.buyer_name_for_item(v_item.item) buyer_name
,v_item.soh
,v_item.length
,v_item.width
,v_item.height
,v_item.weight
,v_variant.variant_id1
,v_variant.value_id1
,v_variant.variant_id2
,v_variant.value_id2
,v_variant.variant_id3
,v_variant.value_id3
,v_variant.variant_id4
,v_variant.value_id4
,v_variant.variant_id5
,v_variant.value_id5
,v_variant.variant_id6
,v_variant.value_id6
FROM (SELECT ia.item
,v_sell.product_id
,MIN(DECODE(row_num,1,selling_point)) selling_point_1
,MIN(DECODE(row_num,2,selling_point)) selling_point_2
,MIN(DECODE(row_num,3,selling_point)) selling_point_3
,MIN(DECODE(row_num,4,selling_point)) selling_point_4
,MIN(DECODE(row_num,5,selling_point)) selling_point_5
,MIN(DECODE(row_num,6,selling_point)) selling_point_6
,MIN(DECODE(row_num,7,selling_point)) selling_point_7
,MIN(DECODE(row_num,8,selling_point)) selling_point_8
,MIN(DECODE(row_num,9,selling_point)) selling_point_9
,MIN(DECODE(row_num,10,selling_point)) selling_point_10
,MIN(DECODE(row_num,11,selling_point)) selling_point_11
,MIN(DECODE(row_num,12,selling_point)) selling_point_12
,MIN(DECODE(row_num,13,selling_point)) selling_point_13
,MIN(DECODE(row_num,14,selling_point)) selling_point_14
,MIN(DECODE(row_num,15,selling_point)) selling_point_15
,MIN(DECODE(row_num,16,selling_point)) selling_point_16
FROM mffecom.item_attr ia
,(SELECT sp.product_id
,row_number () OVER (PARTITION BY sp.product_id ORDER BY sp.selling_point_id) row_num
,sp.selling_point
FROM mffecom.selling_point sp
ORDER BY sp.product_id
,sp.selling_point) v_sell
WHERE ia.product_id = v_sell.product_id
GROUP BY ia.item
,v_sell.product_id) v_selling
,(SELECT NVL(im.item_parent,im.item) item_parent
,im.item
,im.item_desc
,im.dept
,im.class
,im.subclass
,isupp.supplier
,s.sup_name
,isupp.vpn
,miim.stock_on_hand soh
,iscd.length
,iscd.width
,iscd.height
,iscd.weight
,im.diff_1
,di.diff_desc diff1_desc
,im.diff_2
,di2.diff_desc diff2_desc
,im.diff_3
,di3.diff_desc diff3_desc
,im.diff_4
,di4.diff_desc diff4_desc
,dp.delete_type
FROM item_supplier isupp
,sups s
,item_supp_country_dim iscd
,item_master im
,diff_ids di
,diff_ids di2
,diff_ids di3
,diff_ids di4
,daily_purge dp
,merch_item_inv_mv miim
WHERE isupp.item = im.item
AND isupp.item = miim.item (+)
AND isupp.supplier = s.supplier
AND isupp.item = iscd.item
AND isupp.supplier = iscd.supplier
AND isupp.primary_supp_ind = 'Y'
AND iscd.dim_object = 'EA'
AND im.item_level = im.tran_level
AND im.sellable_ind = 'Y'
AND im.diff_1 = di.diff_id (+)
AND im.diff_2 = di2.diff_id (+)
AND im.diff_3 = di3.diff_id (+)
AND im.diff_4 = di4.diff_id (+)
AND im.item = dp.key_value (+)) v_item
,(SELECT ia.item
,pm.product_id
,pm.product_web_desc
,pm.template product_template
,pm.romance_copy
,ia.air_ship_restrict
,ia.do_not_freeze
,ia.free_shipping
,ia.refrigerate
,ia.serial_reqd
,ia.vaccination
,ia.is_consumable
,ia.zero_weight
,pm.no_of_alt_image
,pm.status product_status
,ia.status item_ecom_status
,pm.deactivate_date deactivate_date
,pm.activate_date product_activate_date
,ia.activate_date item_activate_date
,ia.deactivate_date item_deactivate_date
,pm.created_by prod_created_by
,pm.create_datetime prod_created_date
,pm.updated_by prod_updated_by
,ita.created_by item_created_by
,ita.create_date item_created_date
,ia.updated_by item_updated_by
,str.restrict_state
,cd.code_desc
FROM MFFECOM.product_master pm
,MFFECOM.item_attr ia
,MFFECOM.ship_to_restrict str
,rms13.mff_brand mb
,rms13.item_attributes ita
,rms13.code_detail cd
WHERE ia.product_id = pm.product_id
AND ia.item = str.item (+)
AND ia.item = ita.item (+)
AND pm.brand_id = mb.brand_id (+)
AND to_char(ia.fulfillment_method) = cd.code
AND cd.code_type = 'EIFM') v_product
,(SELECT item
,product_id
,MIN(DECODE(row_num,1,variant_id)) variant_id1
,MIN(DECODE(row_num,1,value_id)) value_id1
,MIN(DECODE(row_num,2,variant_id)) variant_id2
,MIN(DECODE(row_num,2,value_id)) value_id2
,MIN(DECODE(row_num,3,variant_id)) variant_id3
,MIN(DECODE(row_num,3,value_id)) value_id3
,MIN(DECODE(row_num,4,variant_id)) variant_id4
,MIN(DECODE(row_num,4,value_id)) value_id4
,MIN(DECODE(row_num,5,variant_id)) variant_id5
,MIN(DECODE(row_num,5,value_id)) value_id5
,MIN(DECODE(row_num,6,variant_id)) variant_id6
,MIN(DECODE(row_num,6,value_id)) value_id6
FROM (SELECT item
,product_id
,row_number () OVER (PARTITION BY item ORDER BY variant_id) row_num
,variant_id
,value_id
FROM mffecom.item_variant)
GROUP BY item
,product_id) v_variant
,item_temp it
WHERE v_item.item = v_selling.item (+)
AND v_item.item = v_product.item (+)
AND v_item.item = v_variant.item (+)
AND v_item.item = it.item
AND v_item.supplier = NVL(:PM_supplier,v_item.supplier)
AND (v_product.prod_created_by = :PM_prod_created_by OR :PM_prod_created_by IS NULL)
AND (v_product.item_created_by = :PM_item_created_by OR :PM_item_created_by IS NULL)
AND (v_product.prod_created_date BETWEEN :prod_created_date_start AND :prod_created_date_to OR :prod_created_date_start IS NULL)
AND (v_product.item_created_date BETWEEN :item_created_date_start AND :item_created_date_to OR :item_created_date_start IS NULL)
ORDER BY v_product.product_id

Faster Query Execution

I have 42 columns and 4 joins in my table. Execution needs 140 seconds, I want to reduce the time of execution. What to do?
SELECT DISTINCT GR_ID,
CONVERT(VARCHAR, GR_DT, 106) AS GRDate,
GR_GN,
GR_GID,
GR_BS,
GR_BN,
GR_DC,
GR_COP,
GR_PA,
GR_AR,
GR_BG,
GR_SM,
GR_KG,
GR_IN,
GR_TS,
CMLC_BC,
CMLC_SC,
CMLC_KG,
CMLC_BL,
CMBC_BC,
CMBC_SC,
CMBC_KG,
CMBC_BL,
GR_BRN,
GR_BLC,
GR_LEA,
GR_ALF,
GR_BTA,
GR_PID,
GR_UTS,
GR_LR,
CMGB_BC,
CMGB_SC,
CMGB_KG,
CMGB_BL,
CMGB_LR,
GR_UPC,
GR_UPT,
GR_UTA,
GR_TTA,
GR_SPA,
GR_SAR,
GR_NR,
ISNULL(ACK_ACK, 'NO') AS ACK_ACK
FROM GR
LEFT JOIN CMBC
ON ( CMBC_ID = GR_GID )
AND ( CMBC_IID = GR_PID )
RIGHT JOIN CMLC
ON ( CMLC_ID = GR_GID )
AND ( CMLC_IID = GR_PID )
RIGHT JOIN CMGB
ON ( CMGB_ID = GR_GID )
AND ( CMGB_IID = GR_PID )
AND ( CMGB_TS = GR_UTS )
LEFT JOIN ACK
ON ( ACK_GN = GR_GN )
AND ( ACK_GID = GR_GID )
ORDER BY GR_GN,
GR_GID

Oracle query works in Toad Developer but not in .net application

This query works in Toad developer but not(wont return results) in an .net application in visual studio. Any clues why?
The query work if I omit the line -- having sum(total_cust_cnt) >= NVL (:customers_out, 100)
select d.region_nbr, pi.city, d.case_id, c.cause_desc cause, sum(total_cust_cnt) customers, d.total_duration_minutes
from t_om_cause_of_trouble c,
t_om_archive_hist_device d,
t_om_archive_hist_loc l,
t_om_pod_info pi
where c.cause = d.cause_of_trouble
and pi.city is not null
and pi.total_cust_cnt > 0
and pi.company = l.company
and pi.distribution_location = l.distribution_location_nbr
and l.company = d.company
and l.region_nbr = d.region_nbr
and l.outage_system = d.outage_system
and l.case_id = d.case_id
and d.first_call_date_time >= :start_date
and d.first_call_date_time <= :end_date
and d.service_request_type = 'LGTS'
and d.cause_of_trouble not in
(select distinct cs.cause from t_om_cause_by_summary cs, t_om_cause_of_trouble c
where summary_cause = 'ERROR' and cs.cause = c.cause)
and d.device_type <> 'ERR'
and d.total_duration_minutes >= 60 * NVL(:hours_out,4)
and d.total_duration_minutes > 5
and d.total_customers_affected >= NVL (:customers_out, 100)
and to_char(d.region_nbr) like :region_nbr
group by d.region_nbr, pi.city, d.case_id, c.cause_desc, d.total_duration_minutes
/* having sum(total_cust_cnt) >= NVL (:customers_out, 100) */
order by d.region_nbr, pi.city, d.case_id
)
group by region_nbr, city, cause
order by region_nbr, city, cause
) stats
where r.region = stats.region_nbr
)

WITH clause causing ORA-32044

I have a query which is used in an OBIEE report which erroring out for "ora-32044 while executing recursive WITH query". Tried running in SQL and it is erroring out in there as well. Can anyone take a look at the query and suggest how it can be run without the WITH clause. I have seen examples but without any level, for a case where WITH has been used in the middle of the query, how can it be rewritten without WITH clause so no ora-32044 occurs
`SELECT 1 dummy, oh.order_number, ol.line_number, ol.shipment_number,
oh.ordered_date, ot.NAME order_type, items.concatenated_segments item,
items.description item_description
,bom.assembly_item
,bom.component_item
,bom.implementation_date
,bom.disable_date as date_disabled
,bom.component_item_description
,ol.flow_status_code line_status
,ol.ordered_quantity,
ol.shipped_quantity,
ol.unit_selling_price,
ol.fulfilled_quantity,
ol.invoiced_quantity,
ol.cancelled_quantity,
ol.schedule_ship_date,
ol.promise_date,
ol.actual_shipment_date,
ol.request_date,
cust_acct.account_number customer_number,
party.party_name customer,
ct.trx_number invoice_number,
ctl.line_number inv_line_number,
ctl.extended_amount,
ct.trx_date invoice_date,
ct.creation_date invoice_creation_date,
wnd.NAME delivery_number,
wdd.released_status,
hp2.party_name endcustomer,
oh.attribute6 business_unit,
TO_CHAR (ol.actual_shipment_date, 'MM/YY') shipped_month,
TO_CHAR (ol.actual_shipment_date, '"Q"Q YY') shipped_qtr,
TO_CHAR (ol.actual_shipment_date, 'YYYY') shipped_year,
mc.segment1 product_group, mc.segment2 product_subgroup,
items.attribute7 drive_capacity,
items.attribute10 drive_form,
items.attribute8 drive_speed,
items.attribute6 drive_type,
items.attribute12 AgileParttype,
items.organization_id,
case when dis.arithmetic_operator = 'NEWPRICE' then operand
when dis.arithmetic_operator = '%'
then (ol.unit_selling_price* ol.ordered_quantity)*dis.operand/100.0
else NVL((NVL (ol.pricing_quantity, 0) * NVL (dis.adjusted_amount, 0)),0) end
discount_amount
FROM apps.oe_order_headers_all oh,
apps.oe_order_lines_all ol,
apps.wsh_new_deliveries wnd,
apps.wsh_delivery_assignments wda,
apps.wsh_delivery_details wdd,
apps.ra_customer_trx_all ct,
apps.ra_customer_trx_lines_all ctl,
apps.oe_transaction_types_tl ot,
(SELECT *
FROM apps.mtl_system_items_vl
WHERE organization_id = 84
) items,
apps.hz_parties party,
apps.hz_cust_accounts cust_acct,
apps.hz_parties hp2,
apps.hz_cust_accounts hca2,
apps.mtl_item_categories mic,
(SELECT *
FROM apps.mtl_category_sets
WHERE category_set_name = 'Inventory') ms,
apps.mtl_categories mc,
(select a.header_id, a.line_id ,a.list_header_id,a.creation_date,
a.arithmetic_operator,a.unit_selling_price,a.operand,
a.ordered_quantity, a.adjusted_amount
from
(select oh.header_id,
ol.line_id,pl.list_header_id,pl.creation_date,opa.arithmetic_operator,
ol.unit_selling_price,opa.operand,
ol.ordered_quantity, opa.adjusted_amount
from apps.oe_price_adjustments opa,
apps.oe_order_lines_all ol,
apps.oe_order_headers_all oh,
apps.qp_list_headers_vl pl
where oh.header_id = opa.header_id
and ol.line_id = opa.line_id
and opa.list_header_id = pl.list_header_id
and opa.applied_flag = 'Y'
AND opa.list_line_type_code = 'DIS') a,
(select oh.header_id, ol.line_id,max(pl.creation_date) creation_date, min(operand)
operand
from apps.oe_price_adjustments opa,
apps.oe_order_lines_all ol,
apps.oe_order_headers_all oh,
apps.qp_list_headers_vl pl
where oh.header_id = opa.header_id
and ol.line_id = opa.line_id
and opa.list_header_id = pl.list_header_id
and opa.applied_flag = 'Y'
AND opa.list_line_type_code = 'DIS'
group by oh.header_id, ol.line_id) b
where
a.header_id = b.header_id
and a.line_id = b.line_id
and a.creation_date = b.creation_date
and a.operand = b.operand ) dis
,(
WITH recursiveBOM (parent_item_id,parent_item,parent_org_id,assembly_item_id,
assembly_item,assembly_org_id,
component_item_id,component_item, component_item_description,implementation_date,
disable_date) AS
(
SELECT item.inventory_item_id parent_item_id
,item.segment1 parent_item
,item.organization_id parent_org_id
,bom.assembly_item_id
,msi.segment1 assembly_item
,bom.organization_id assembly_org_id
,bic.component_item_id
,msi2.segment1 component_item
,msi2.description component_item_description
,bic.implementation_date
,bic.disable_date
FROM apps.mtl_system_items_b item, apps.bom_bill_of_materials_v bom,
apps.bom_inventory_components_v bic, apps.mtl_system_items msi,
apps.mtl_system_items msi2
WHERE 1=1
AND bom.assembly_item_id = item.inventory_item_id
AND bom.organization_id = item.organization_id
AND bic.bill_sequence_id = bom.common_bill_sequence_id
AND msi.inventory_item_id = bom.assembly_item_id
AND msi.organization_id = bom.organization_id
AND msi2.inventory_item_id = bic.component_item_id
AND msi2.organization_id = msi.organization_id
AND bic.implementation_date IS NOT NULL
AND item.organization_id = 84
AND item.inventory_item_id > 0
UNION ALL
SELECT parent.parent_item_id
,parent.parent_item
,parent.parent_org_id
,bom.assembly_item_id
,msi.segment1 assembly_item
,bom.organization_id assembly_org_id
,bic.component_item_id
,msi2.segment1 component_item
,msi2.description component_item_description
,bic.implementation_date
,bic.disable_date
FROM recursiveBOM parent, apps.bom_bill_of_materials_v bom,
apps.bom_inventory_components_v bic, apps.mtl_system_items msi,
apps.mtl_system_items msi2
WHERE 1=1
AND bic.bill_sequence_id = bom.common_bill_sequence_id
AND msi.inventory_item_id = bom.assembly_item_id
AND msi.organization_id = bom.organization_id
AND msi2.inventory_item_id = bic.component_item_id
AND msi2.organization_id = msi.organization_id
AND bic.implementation_date IS NOT NULL
AND bom.assembly_item_id = parent.component_item_id
AND bom.organization_id = parent.assembly_org_id
AND bom.organization_id = 84
AND parent.assembly_org_id = 84
)
SELECT DISTINCT parent_item_id,parent_item,parent_org_id
,assembly_item_id
,assembly_item
,assembly_org_id
,component_item_id
,component_item
,component_item_description
,implementation_date
,disable_date
FROM recursiveBOM
) bom
WHERE oh.header_id = ol.header_id
AND ol.line_id = wdd.source_line_id
AND wdd.source_code = 'OE'
AND wda.delivery_detail_id = wdd.delivery_detail_id
AND wda.delivery_id = wnd.delivery_id
AND ol.line_id = ctl.interface_line_attribute6(+)
AND ctl.customer_trx_id = ct.customer_trx_id(+)
AND oh.order_type_id = ot.transaction_type_id
AND ol.inventory_item_id = items.inventory_item_id
AND ol.sold_to_org_id = cust_acct.cust_account_id
AND cust_acct.party_id = party.party_id
AND oh.end_customer_id = hca2.cust_account_id(+)
AND hca2.party_id = hp2.party_id(+)
AND items.inventory_item_id = mic.inventory_item_id(+)
AND items.organization_id = mic.organization_id(+)
AND mic.category_id = mc.category_id(+)
AND mic.category_set_id = ms.category_set_id(+)
and ol.line_id = dis.line_id(+)
and ol.header_id = dis.header_id(+)
AND bom.parent_item_id (+) = items.inventory_item_id
AND bom.parent_org_id (+) = items.organization_id `

Progress DB, need to merge two queries

I have 2 progress database queries and I'm trying to merge them into one statement, but I am getting errors. Each of these queries simply returns a number and I would like to sum those 2 numbers together. Either that or make another query from scratch. They both take in a set of value codes for "DM1" and they both accept 1 "product".
Query 1
SELECT SUM(opn3.samt)
FROM PUB.ord ord3, PUB.opn opn3
WHERE
ord3.subsnum = opn3.subsnum
AND ord3.onum = opn3.onum
AND ord3.DM1 != ''
AND ord3.DM1 IN('XCWAJC25','WCWAMO73')
AND ord3.prdcde = 'CSC'
AND ord3.stat != 16
AND opn3.samt >= 0
GROUP BY ord3.DM1, ord3.prdcde
Query 2
SELECT SUM((-1 * opn2.samt) + ord2.samt)
FROM PUB.ord ord2, PUB.opn opn2
WHERE
ord2.subsnum = opn2.subsnum
AND ord2.onum = opn2.onum
AND ord2.DM1 != ''
AND ord2.DM1 IN('XCWAJC25','WCWAMO73')
AND ord2.prdcde = 'CSC'
AND ord2.stat = 16
AND opn2.samt < 0
GROUP BY ord2.DM1, ord2.prdcde
Merge attempt so far...
SELECT SUM(opn3.samt + (SELECT SUM((-1 * opn2.samt) + ord2.samt)
FROM PUB.ord ord2, PUB.opn opn2
WHERE
ord2.subsnum = opn2.subsnum
AND ord2.onum = opn2.onum
AND ord2.DM1 != ''
AND ord2.DM1 = ord3.DM1
AND ord2.prdcde = ord3.prdcde
AND ord2.stat = 16
AND opn2.samt < 0
GROUP BY ord2.DM1, ord2.prdcde
)) as foo
FROM PUB.ord ord3, PUB.opn opn3
WHERE
ord3.subsnum = opn3.subsnum
AND ord3.onum = opn3.onum
AND ord3.DM1 != ''
AND ord3.DM1 IN('XCWAJC25','WCWAMO73')
AND ord3.prdcde = 'CSC'
AND ord3.stat != 16
AND opn3.samt >= 0
GROUP BY ord3.DM1, ord3.prdcde
Thanks
I think this will work, although it would be nice to have sample data to verify:
SELECT COALESCE(SUM(a.samt), 0) - COALESCE(SUM(b.samt), 0)
+ COALESCE(SUM(CASE WHEN ord.stat = 16
AND b.samt < 0
THEN ord.samt END), 0)
FROM PUB.ord ord
LEFT JOIN PUB.opn a
ON a.subsnum = ord.subsnum
AND a.onum = ord.onum
AND a.samt >= 0
AND ord.stat != 16
LEFT JOIN PUB.opn b
ON b.subsnum = ord.subsnum
AND b.onum = ord.onum
AND b.samt < 0
AND ord.stat = 16
WHERE ord.DM1 IN('XCWAJC25', 'WCWAMO73')
AND ord.prdcde = 'CSC'
GROUP BY ord.DM1
Notes on query/stuff:
Always explicitly qualify joins, don't use the comma-separated FROM clause
I don't think you needed ord.DM1 != '', given that values have to be in a specific set
Putting a clause into a LEFT JOIN condition instead of the WHERE clause has a slightly different effect; it adds the condition to the join, instead of the filtering. This means that rows can be excluded based on something in the left table, regardless of whether you need the actual row (this is why ord.stat ended up in the LEFT JOINs). INNER JOINs would technically behave the same way, but it isn't usually noticeable because causing the right table to be excluded also excludes the left table.
I think this should do the trick, given the the individual queries work as intended:
SELECT sum1.tot + sum2.tot
FROM
(SELECT SUM(opn3.samt) as tot
FROM PUB.ord ord3, PUB.opn opn3
WHERE
ord3.subsnum = opn3.subsnum
AND ord3.onum = opn3.onum
AND ord3.DM1 != ''
AND ord3.DM1 IN('XCWAJC25','WCWAMO73')
AND ord3.prdcde = 'CSC'
AND ord3.stat != 16
AND opn3.samt >= 0
GROUP BY ord3.DM1, ord3.prdcde) sum1,
(SELECT SUM((-1 * opn2.samt) + ord2.samt) as tot
FROM PUB.ord ord2, PUB.opn opn2
WHERE
ord2.subsnum = opn2.subsnum
AND ord2.onum = opn2.onum
AND ord2.DM1 != ''
AND ord2.DM1 IN('XCWAJC25','WCWAMO73')
AND ord2.prdcde = 'CSC'
AND ord2.stat = 16
AND opn2.samt < 0
GROUP BY ord2.DM1, ord2.prdcde) sum2