SubQuery added to existing Query in PL/SQL - sql

I am sure this is simple to do and I am missing something on formatting but I want to add the following query:
SELECT s.stud_id,
jb.REGION,
jt.DISTRICT
FROM pa_student s,
(SELECT su.stud_id,
rf.user_desc REGION
FROM pa_stud_user su,
pa_usrrf_stud rf
WHERE su.col_num = rf.col_num
AND su.user_value = rf.user_id
AND su.col_num = 300) JB,
(SELECT su.stud_id,
rf.user_desc DISTRICT
FROM pa_stud_user su,
pa_usrrf_stud rf
WHERE su.col_num = rf.col_num
AND su.user_value = rf.user_id
AND su.col_num = 400) JT
WHERE s.stud_id = jb.stud_id (+)
AND s.stud_id = jt.stud_id (+)
To the existing query (both of these work fine by themselves)
SELECT sqc.CPNT_TYP_ID AS cpntTypeID ,
sqc.CPNT_ID AS cpntID ,
sqc.REV_DTE AS revDate,
sqc.COMPL_DTE AS complDate,
sqc.CMPL_STAT_ID AS cmplStatID,
sqc.REQ_DTE AS reqDate,
cpt.CPNT_TITLE AS cpntTtile,
cpt.RTYP_ID ,
cpt.DMN_ID ,
cpt.DEL_MTH_ID ,
cpt.CPNT_SRC_ID ,
cpt.NOTACTIVE ,
cpt.SAFETY_REL ,
cpt.CREATE_DTE,
cpt.REV_NUM AS cpntRevNum,
cpt.REVISER ,
cpt.APPRVR ,
cpt.APPRVD ,
cpt.APPRVL_DTE ,
cpt.MIN_ENRL ,
cpt.MAX_ENRL ,
cpt.CPNT_LEN ,
cpt.PREP_LEN ,
cpt.LES_PLAN ,
cpt.CONTACT ,
cpt.CREDIT_HRS,
cpt.CPE_HRS ,
cpt.CONTACT_HRS ,
cpt.STUD_MATS ,
cpt.INST_MATS ,
cpt.CPNT_DESC ,
cpt.TGT_AUDNC ,
cpt.COMMENTS ,
cpt.GRADE_OPT ,
cpt.ENRL_THRESHOLD_DAYS ,
cpt.SHIPPING_REQUIRED,
cpt.AUTO_FILL_ENRL ,
cpt.WAITLIST_REMDR_SENT,
cpt.AUTO_COMPETENCY ,
cpt.CPNT_CLASSIFICATION ,
cpt.CHGBCK_METHOD ,
cpt.CATALOG_SKU ,
cpt.SCHEDULE_CAN_OVERRIDE_PRICE ,
cpt.PRODUCTION_READY,
cpt.CPNT_GOALS ,
cpt.SELF_RECORD_LRNGEVT ,
cpt.SUB_RECORD_LRNGEVT ,
cpt.TAP_DEF_ID ,
cpt.APPROVAL_REQD ,
cpt.INIT_PERIOD_TYPE_ID ,
cpt.INIT_NUMBER ,
cpt.INIT_BASIS_TYPE_ID,
cpt.RETRNG_PERIOD_TYPE_ID ,
cpt.RETRNG_NUMBER ,
cpt.RETRNG_BASIS_TYPE_ID ,
cpt.RATING ,
cpt.SELF_ENRL ,
cpt.USER_REQ_ENABLED ,
cpt.USER_CAN_WAITLIST ,
cpt.CPNT_KEY,
cpt.REGISTER_ONLINE ,
cpt.ESIG_ENABLED ,
cs.CMPL_STAT_DESC AS cmplStatDesc,
s.stud_id,
s.fname,
s.lname,
s.mi,
s.STUD_ID AS studID,
s.LNAME AS lastName,
s.FNAME AS firstName,
s.MI AS middleName,
s.EMP_STAT_ID ,
s.EMP_TYP_ID,
s.JL_ID ,
s.JP_ID ,
s.TARGET_JP_ID ,
s.JOB_TITLE ,
s.DMN_ID ,
s.ORG_ID,
s.REGION_ID,
s.CO_ID ,
DECODE(s.NOTACTIVE, 'Y','N','N','Y') AS studActive ,
s.ADDR ,
s.CITY ,
s.STATE ,
s.POSTAL ,
s.CNTRY ,
s.SUPER ,
s.COACH_STUD_ID ,
s.HIRE_DTE,
s.TERM_DTE ,
s.EMAIL_ADDR,
s.RESUME_LOCN ,
s.COMMENTS ,
s.SHIPPING_NAME ,
s.SHIPPING_CONTACT_NAME,
s.SHIPPING_ADDR ,
s.SHIPPING_ADDR1 ,
s.SHIPPING_CITY ,
s.SHIPPING_STATE,
s.SHIPPING_POSTAL ,
s.SHIPPING_CNTRY ,
s.SHIPPING_PHON_NUM ,
s.SHIPPING_FAX_NUM ,
s.SHIPPING_EMAIL_ADDR ,
s.STUD_PSWD ,
s.PIN ,
s.PIN_DATE,
s.ENCRYPTED ,
s.HAS_ACCESS ,
s.BILLING_NAME ,
s.BILLING_CONTACT_NAME ,
s.BILLING_ADDR ,
s.BILLING_ADDR1 ,
s.BILLING_CITY ,
s.BILLING_STATE ,
s.BILLING_POSTAL,
s.BILLING_CNTRY ,
s.BILLING_PHON_NUM ,
s.BILLING_FAX_NUM ,
s.BILLING_EMAIL_ADDR ,
s.SELF_REGISTRATION ,
s.SELF_REGISTRATION_DATE,
s.ACCESS_TO_ORG_FIN_ACT ,
s.NOTIFY_DEV_PLAN_ITEM_ADD ,
s.NOTIFY_DEV_PLAN_ITEM_MOD ,
s.NOTIFY_DEV_PLAN_ITEM_REMOVE ,
s.NOTIFY_WHEN_SUB_ITEM_COMPLETE ,
s.NOTIFY_WHEN_SUB_ITEM_FAILURE ,
s.LOCKED ,
s.PASSWORD_EXP_DATE,
s.SECURITY_QUESTION ,
s.SECURITY_ANSWER ,
s.ROLE_ID ,
s.IMAGE_ID ,
s.GENDER ,
s.PAST_SERVICE,
s.LST_UNLOCK_TSTMP,
s.MANAGE_SUB_SP,
s.MANAGE_OWN_SP,
NVL(userPref.preferred_timezone,pkg_state.get_default_timezone) AS preferred_timezone,
sq.qual_id AS qualID,
sq.assgn_dte AS assignDate,
sq.qual_id_root AS qualIDRoot,
q.qual_title AS qualTitle,
q.DMN_ID AS qualDomain,
q.QUAL_TYP_ID AS qualTypeID ,
q.NOTACTIVE AS qualNotActive ,
q.CREATE_DTE AS qualCreateDate,
q.QUAL_DESC AS qualDesc,
q.FORCE_INCOMPLETE AS qualForceIncomplete,
q.BASIS_DATE AS qualBasisDate,
q.ESIG_ENABLED AS qualEsigEnabled,
pkg_student.get_stud_qual_status (sq.stud_id, sq.qual_id, sq.qual_id_root) AS complete
FROM PA_STUD_QUAL_CPNT sqc,
PA_CPNT cpt,
PA_CMPL_STAT cs,
pa_student s,
pa_user_preference userPref,
pa_qual q,
(SELECT sq.stud_id,
sq.qual_id,
sq.assgn_dte,
sq.qual_id_root,
row_number() over (partition BY sq.stud_id,sq.qual_id order by sq.assgn_dte) rnum
FROM
(SELECT sq.stud_id,
sq.qual_id,
sq.assgn_dte,
sq.qual_id_root
FROM pa_stud_qual_relation sq,
( SELECT sq.stud_id FROM pa_stud_qual sq WHERE 1=1
/** and sq.stud_id in [UserSearch] */
/** and sq.qual_id in [CurriculumSearch] */
UNION
SELECT sq.stud_id
FROM pa_stud_qual_relation sq
WHERE 1=1
/** and sq.stud_id in [UserSearch] */
/** and sq.qual_id in [CurriculumSearch] */
) students
WHERE sq.stud_id = students.stud_id
/** and sq.qual_id in [CurriculumSearch]*/
UNION ALL
SELECT sq.stud_id,
sq.qual_id,
sq.assgn_dte,
sq.qual_id_root
FROM pa_stud_qual_relation sq,
pa_qual_relation qr,
( SELECT sq.stud_id FROM pa_stud_qual sq WHERE 1=1
/** and sq.stud_id in [UserSearch] */
/** and sq.qual_id in [CurriculumSearch] */
UNION
SELECT sq.stud_id
FROM pa_stud_qual_relation sq
WHERE 1=1
/** and sq.stud_id in [UserSearch] */
/** and sq.qual_id in [CurriculumSearch] */
) students
WHERE sq.stud_id = students.stud_id
AND qr.qual_id_child = sq.qual_id
/** and qr.qual_id_parent in [CurriculumSearch]*/
) sq
) sq
WHERE sqc.STUD_ID (+) = sq.stud_id
AND sqc.QUAL_ID (+) = sq.qual_id
AND sqc.QUAL_ID_ROOT(+) = sq.qual_id_root
AND sqc.CPNT_TYP_ID = cpt.CPNT_TYP_ID (+)
AND sqc.CPNT_ID = cpt.CPNT_ID (+)
AND sqc.REV_DTE = cpt.REV_DTE (+)
AND sqc.CMPL_STAT_ID = cs.CMPL_STAT_ID (+)
AND s.stud_id = sq.stud_id
AND s.stud_id = userPref.user_id(+)
AND userPref.user_type(+) = 'S'
AND sq.rnum = 1
AND sq.qual_id = q.qual_id
/** and [security:pa_student s]*/
I have tried so many different formats that I am confused beyond my knowledge of SQL... any ideas? Thanks in advance.

I combined it. It took great pain. Please try this query and good luck:
SELECT sqc.CPNT_TYP_ID AS cpntTypeID,
sqc.CPNT_ID AS cpntID,
sqc.REV_DTE AS revDate,
sqc.COMPL_DTE AS complDate,
sqc.CMPL_STAT_ID AS cmplStatID,
sqc.REQ_DTE AS reqDate,
cpt.CPNT_TITLE AS cpntTtile,
cpt.RTYP_ID,
cpt.DMN_ID,
cpt.DEL_MTH_ID,
cpt.CPNT_SRC_ID,
cpt.NOTACTIVE,
cpt.SAFETY_REL,
cpt.CREATE_DTE,
cpt.REV_NUM AS cpntRevNum,
cpt.REVISER,
cpt.APPRVR,
cpt.APPRVD,
cpt.APPRVL_DTE,
cpt.MIN_ENRL,
cpt.MAX_ENRL,
cpt.CPNT_LEN,
cpt.PREP_LEN,
cpt.LES_PLAN,
cpt.CONTACT,
cpt.CREDIT_HRS,
cpt.CPE_HRS,
cpt.CONTACT_HRS,
cpt.STUD_MATS,
cpt.INST_MATS,
cpt.CPNT_DESC,
cpt.TGT_AUDNC,
cpt.COMMENTS,
cpt.GRADE_OPT,
cpt.ENRL_THRESHOLD_DAYS,
cpt.SHIPPING_REQUIRED,
cpt.AUTO_FILL_ENRL,
cpt.WAITLIST_REMDR_SENT,
cpt.AUTO_COMPETENCY,
cpt.CPNT_CLASSIFICATION,
cpt.CHGBCK_METHOD,
cpt.CATALOG_SKU,
cpt.SCHEDULE_CAN_OVERRIDE_PRICE,
cpt.PRODUCTION_READY,
cpt.CPNT_GOALS,
cpt.SELF_RECORD_LRNGEVT,
cpt.SUB_RECORD_LRNGEVT,
cpt.TAP_DEF_ID,
cpt.APPROVAL_REQD,
cpt.INIT_PERIOD_TYPE_ID,
cpt.INIT_NUMBER,
cpt.INIT_BASIS_TYPE_ID,
cpt.RETRNG_PERIOD_TYPE_ID,
cpt.RETRNG_NUMBER,
cpt.RETRNG_BASIS_TYPE_ID,
cpt.RATING,
cpt.SELF_ENRL,
cpt.USER_REQ_ENABLED,
cpt.USER_CAN_WAITLIST,
cpt.CPNT_KEY,
cpt.REGISTER_ONLINE,
cpt.ESIG_ENABLED,
cs.CMPL_STAT_DESC AS cmplStatDesc,
s.stud_id,
s.fname,
s.lname,
s.mi,
s.STUD_ID AS studID,
s.LNAME AS lastName,
s.FNAME AS firstName,
s.MI AS middleName,
s.EMP_STAT_ID,
s.EMP_TYP_ID,
s.JL_ID,
s.JP_ID,
s.TARGET_JP_ID,
s.JOB_TITLE,
s.DMN_ID,
s.ORG_ID,
s.REGION_ID,
s.CO_ID,
DECODE(s.NOTACTIVE, 'Y','N','N','Y') AS studActive,
s.ADDR,
s.CITY,
s.STATE,
s.POSTAL,
s.CNTRY,
s.SUPER,
s.COACH_STUD_ID,
s.HIRE_DTE,
s.TERM_DTE,
s.EMAIL_ADDR,
s.RESUME_LOCN,
s.COMMENTS,
s.SHIPPING_NAME,
s.SHIPPING_CONTACT_NAME,
s.SHIPPING_ADDR,
s.SHIPPING_ADDR1,
s.SHIPPING_CITY,
s.SHIPPING_STATE,
s.SHIPPING_POSTAL,
s.SHIPPING_CNTRY,
s.SHIPPING_PHON_NUM,
s.SHIPPING_FAX_NUM,
s.SHIPPING_EMAIL_ADDR,
s.STUD_PSWD,
s.PIN,
s.PIN_DATE,
s.ENCRYPTED,
s.HAS_ACCESS,
s.BILLING_NAME,
s.BILLING_CONTACT_NAME,
s.BILLING_ADDR,
s.BILLING_ADDR1,
s.BILLING_CITY,
s.BILLING_STATE,
s.BILLING_POSTAL,
s.BILLING_CNTRY,
s.BILLING_PHON_NUM,
s.BILLING_FAX_NUM,
s.BILLING_EMAIL_ADDR,
s.SELF_REGISTRATION,
s.SELF_REGISTRATION_DATE,
s.ACCESS_TO_ORG_FIN_ACT,
s.NOTIFY_DEV_PLAN_ITEM_ADD,
s.NOTIFY_DEV_PLAN_ITEM_MOD,
s.NOTIFY_DEV_PLAN_ITEM_REMOVE,
s.NOTIFY_WHEN_SUB_ITEM_COMPLETE,
s.NOTIFY_WHEN_SUB_ITEM_FAILURE,
s.LOCKED,
s.PASSWORD_EXP_DATE,
s.SECURITY_QUESTION,
s.SECURITY_ANSWER,
s.ROLE_ID,
s.IMAGE_ID,
s.GENDER,
s.PAST_SERVICE,
s.LST_UNLOCK_TSTMP,
s.MANAGE_SUB_SP,
s.MANAGE_OWN_SP,
NVL(userPref.preferred_timezone,pkg_state.get_default_timezone) AS preferred_timezone,
sq.qual_id AS qualID,
sq.assgn_dte AS assignDate,
sq.qual_id_root AS qualIDRoot,
q.qual_title AS qualTitle,
q.DMN_ID AS qualDomain,
q.QUAL_TYP_ID AS qualTypeID,
q.NOTACTIVE AS qualNotActive,
q.CREATE_DTE AS qualCreateDate,
q.QUAL_DESC AS qualDesc,
q.FORCE_INCOMPLETE AS qualForceIncomplete,
q.BASIS_DATE AS qualBasisDate,
q.ESIG_ENABLED AS qualEsigEnabled,
pkg_student.get_stud_qual_status (sq.stud_id, sq.qual_id, sq.qual_id_root) AS complete,
/**-- Added from smaller query */
jb.REGION,
jt.DISTRICT
/**-- Added from smaller query */
FROM PA_STUD_QUAL_CPNT sqc,
PA_CPNT cpt,
PA_CMPL_STAT cs,
pa_student s,
pa_user_preference userPref,
pa_qual q,
(
SELECT sq.stud_id, sq.qual_id, sq.assgn_dte, sq.qual_id_root, row_number() over (partition BY sq.stud_id,sq.qual_id order by sq.assgn_dte) rnum
FROM
(
SELECT sq.stud_id, sq.qual_id, sq.assgn_dte, sq.qual_id_root
FROM pa_stud_qual_relation sq,
(
SELECT sq.stud_id FROM pa_stud_qual sq WHERE 1=1
/** and sq.stud_id in [UserSearch] */
/** and sq.qual_id in [CurriculumSearch] */
UNION
SELECT sq.stud_id FROM pa_stud_qual_relation sq WHERE 1=1
/** and sq.stud_id in [UserSearch] */
/** and sq.qual_id in [CurriculumSearch] */
) students
WHERE sq.stud_id = students.stud_id
/** and sq.qual_id in [CurriculumSearch]*/
UNION ALL
SELECT sq.stud_id, sq.qual_id, sq.assgn_dte, sq.qual_id_root
FROM pa_stud_qual_relation sq,
pa_qual_relation qr,
(
SELECT sq.stud_id FROM pa_stud_qual sq WHERE 1=1
/** and sq.stud_id in [UserSearch] */
/** and sq.qual_id in [CurriculumSearch] */
UNION
SELECT sq.stud_id FROM pa_stud_qual_relation sq WHERE 1=1
/** and sq.stud_id in [UserSearch] */
/** and sq.qual_id in [CurriculumSearch] */
) students
WHERE sq.stud_id = students.stud_id
AND qr.qual_id_child = sq.qual_id
/** and qr.qual_id_parent in [CurriculumSearch]*/
) sq
) sq,
/**-- Added from smaller query */
(
SELECT su.stud_id, rf.user_desc as REGION
FROM pa_stud_user su
JOIN pa_usrrf_stud rf on rf.col_num = su.col_num
WHERE su.user_value = rf.user_id
AND su.col_num = 300
) JB,
(
SELECT su.stud_id, rf.user_desc as DISTRICT
FROM pa_stud_user su
JOIN pa_usrrf_stud rf on rf.col_num = su.col_num
WHERE su.user_value = rf.user_id
AND su.col_num = 400
) JT
/**-- Added from smaller query */
WHERE sqc.STUD_ID = sq.stud_id
AND sqc.QUAL_ID = sq.qual_id
AND sqc.QUAL_ID_ROOT = sq.qual_id_root
AND sqc.CPNT_TYP_ID = cpt.CPNT_TYP_ID
AND sqc.CPNT_ID = cpt.CPNT_ID
AND sqc.REV_DTE = cpt.REV_DTE
AND sqc.CMPL_STAT_ID = cs.CMPL_STAT_ID
AND s.stud_id = sq.stud_id
AND s.stud_id = userPref.user_id
AND userPref.user_type = 'S'
AND sq.rnum = 1
AND sq.qual_id = q.qual_id
/**-- Added from smaller query */
AND s.stud_id = jb.stud_id
AND s.stud_id = jt.stud_id
/**-- Added from smaller query */
/** and [security:pa_student s]*/

This is simpler than you think.
Option One (recommended)
Turn the simplified first query into a function (probably embedded in another package)
FUNCTION get_user_desc(p_stud_id NUMBER,p_col_num NUMBER) RETURN VARCHAR2 IS
BEGIN
FOR q_rec IN (SELECT rf.user_desc user_desc
FROM pa_stud_user su,
pa_usrrf_stud rf
WHERE su.col_num = rf.col_num
AND su.user_value = rf.user_id
AND su.col_num = p_col_num
AND su.stud_id = p_stud_id
) LOOP
RETURN q_rec.user_desc;
END LOOP;
RETURN NULL;
END;
And Then add the two columns to the "Original Query"
get_user_desc(sq.stud_id,300) AS region,
get_user_desc(sq.stud_id,400) AS district
Option 2 if you don't want to create the function above
Add two new queries as new columns in the second query.
SELECT
... ...
( SELECT rf.user_desc as region
FROM pa_stud_user su,
pa_usrrf_stud rf
WHERE su.col_num = rf.col_num
AND su.user_value = rf.user_id
AND su.col_num = 300
AND su.stud_id = sq.stud_id
AND ROWNUM=1) AS region,
( SELECT rf.user_desc as region
FROM pa_stud_user su,
pa_usrrf_stud rf
WHERE su.col_num = rf.col_num
AND su.user_value = rf.user_id
AND su.col_num = 400
AND su.stud_id = sq.stud_id
AND ROWNUM=1) AS district
...
Just that simple.

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

Select or Join tatement for BIRT Report using SQL

I am just getting back into writing SQL Select Statements and I am have an issue. I have an existing report that works perfectly. However, I was asked to add some fields in the result set. The filed is in a table with a foreign key relationship. I am trying to add it to the query and I am failing. I can write the select statement separately and it returns the right results. I just don't know how to add it to the existing statement. I have tried left joins, adding it to the select and where but the result set is wrong. I would like to add the following to return the info if available to the result set in a new column but not prevent other data if null.
I would like to add:
select sc.FACILITY_ID, ce.schd_id
from PA_SCHED sc, PA_CPNT_EVTHST ce
where sc.SCHD_ID = ce.schd_id
To the following:
select s.stud_id as studID, s.lname as lastName, s.fname as firstName, s.mi
as middleName,
s.EMP_STAT_ID ,s.EMP_TYP_ID ,s.JL_ID ,s.JP_ID ,s.TARGET_JP_ID ,
s.JOB_TITLE ,s.DMN_ID ,s.ORG_ID ,s.REGION_ID, s.CO_ID , DECODE
(s.NOTACTIVE,'Y','N','N','Y') as active ,
s.ADDR ,s.CITY ,s.STATE ,s.POSTAL ,s.CNTRY, s.SUPER , s.COACH_STUD_ID ,
s.HIRE_DTE,s.TERM_DTE ,s.EMAIL_ADDR, s.RESUME_LOCN ,s.COMMENTS ,
s.SHIPPING_NAME ,
s.SHIPPING_CONTACT_NAME,
s.SHIPPING_ADDR ,s.SHIPPING_ADDR1 ,s.SHIPPING_CITY ,
s.SHIPPING_STATE,
s.SHIPPING_POSTAL ,s.SHIPPING_CNTRY ,s.SHIPPING_PHON_NUM,
s.SHIPPING_FAX_NUM,
s.SHIPPING_EMAIL_ADDR ,s.STUD_PSWD ,s.PIN ,s.PIN_DATE,
s.ENCRYPTED , s.HAS_ACCESS ,s.BILLING_NAME , s.BILLING_CONTACT_NAME ,
s.BILLING_ADDR ,s.BILLING_ADDR1 ,s.BILLING_CITY ,s.BILLING_STATE ,
s.BILLING_POSTAL,
s.BILLING_CNTRY ,
s.BILLING_PHON_NUM ,s.BILLING_FAX_NUM ,s.BILLING_EMAIL_ADDR ,
s.SELF_REGISTRATION ,s.SELF_REGISTRATION_DATE, s.ACCESS_TO_ORG_FIN_ACT ,
s.NOTIFY_DEV_PLAN_ITEM_ADD , s.NOTIFY_DEV_PLAN_ITEM_MOD ,
s.NOTIFY_DEV_PLAN_ITEM_REMOVE ,
s.NOTIFY_WHEN_SUB_ITEM_COMPLETE ,s.NOTIFY_WHEN_SUB_ITEM_FAILURE ,
s.LOCKED ,s.PASSWORD_EXP_DATE,s.SECURITY_QUESTION ,
s.SECURITY_ANSWER ,s.ROLE_ID ,s.IMAGE_ID ,s.GENDER ,s.PAST_SERVICE,
s.LST_UNLOCK_TSTMP,
s.LST_UPD_USR, s.LST_UPD_TSTMP,s.MANAGE_SUB_SP, s.MANAGE_OWN_SP,
e.cpnt_typ_id as cpntTypeID, e.cpnt_id as cpntID, e.rev_dte as revDate,
e.rev_num as revNum,
e.schd_id as schedID, cp.cpnt_title as cpntDesc, e.grade,
e.compl_dte as complDate, e.cmpl_stat_id as complStatID,
cs.cmpl_stat_desc as complStatDesc, nvl(e.total_hrs, 0) as totalHrs,
nvl(e.credit_hrs, 0) as creditHrs, nvl(e.contact_hrs, 0) as contactHrs,
nvl(e.cpe_hrs, 0) as cpeHrs, nvl(oi.price, 0) as tuition,
e.inst_name as instName, e.comments as eventComments, e.es_user_name as
esUserName,
e.lst_upd_tstmp as lstTmsp,
e.esig_meaning_code_id || ' ' || e.esig_meaning_code_desc as
esigMeaningCode,
e.currency_code as currencyCode, cur.symbol as currencySymbol,
cur.description,
cur.active, cur.is_default, cur.label_id
,s.jl_id
,s.job_title
,s.super
,s.hire_dte
,s.term_dte
,eeno.user_value
,co.user_value
from pa_student s, PA_CPNT_EVTHST e, PA_CMPL_STAT cs,
pa_fin_order_item oi, pa_currency cur, pa_cpnt cp,
(SELECT s.stud_id, f.user_value
FROM PA_STUD_USER f, pa_student s
WHERE s.stud_id = f.stud_id (+)
AND f.col_num (+) = '10') eeno
,(SELECT s.stud_id, f.user_value
FROM PA_STUD_USER f, pa_student s
WHERE s.stud_id = f.stud_id (+)
AND f.col_num (+) = '40') co
where e.stud_id = s.stud_id
and e.cmpl_stat_id = cs.cmpl_stat_id
and e.currency_code = cur.currency_code(+)
and e.order_item_id = oi.order_item_id(+)
and cp.cpnt_typ_id(+) = e.cpnt_typ_id
and cp.cpnt_id(+) = e.cpnt_id
and cp.rev_dte(+) = e.rev_dte
and s.stud_id = eeno.stud_id(+)
AND s.stud_id = co.stud_id(+)
/** and trunc(e.compl_dte) >= [FromDate]
and not trunc(e.compl_dte) > [ToDate]
and s.stud_id in [UserSearch]
and e.cpnt_typ_id in [ItemTypeSearch]
and [security:pa_student s] */
Thank you for your help.
Added this in FROM ...
(select sc.FACILITY_ID, ce.schd_id from PA_SCHED sc
where sc.SCHD_ID (+) = e.schd_id
) sc
and appended sc.FACILITY_ID in the main SELECT ...
See if this works ...
SELECT s.stud_id AS studID,
s.lname AS lastName,
s.fname AS firstName,
s.mi AS middleName,
s.EMP_STAT_ID ,
s.EMP_TYP_ID ,
s.JL_ID ,
s.JP_ID ,
s.TARGET_JP_ID ,
s.JOB_TITLE ,
s.DMN_ID ,
s.ORG_ID ,
s.REGION_ID,
s.CO_ID ,
DECODE (s.NOTACTIVE,'Y','N','N','Y') AS active ,
s.ADDR ,
s.CITY ,
s.STATE ,
s.POSTAL ,
s.CNTRY,
s.SUPER ,
s.COACH_STUD_ID ,
s.HIRE_DTE,
s.TERM_DTE ,
s.EMAIL_ADDR,
s.RESUME_LOCN ,
s.COMMENTS ,
s.SHIPPING_NAME ,
s.SHIPPING_CONTACT_NAME,
s.SHIPPING_ADDR ,
s.SHIPPING_ADDR1 ,
s.SHIPPING_CITY ,
s.SHIPPING_STATE,
s.SHIPPING_POSTAL ,
s.SHIPPING_CNTRY ,
s.SHIPPING_PHON_NUM,
s.SHIPPING_FAX_NUM,
s.SHIPPING_EMAIL_ADDR ,
s.STUD_PSWD ,
s.PIN ,
s.PIN_DATE,
s.ENCRYPTED ,
s.HAS_ACCESS ,
s.BILLING_NAME ,
s.BILLING_CONTACT_NAME ,
s.BILLING_ADDR ,
s.BILLING_ADDR1 ,
s.BILLING_CITY ,
s.BILLING_STATE ,
s.BILLING_POSTAL,
s.BILLING_CNTRY ,
s.BILLING_PHON_NUM ,
s.BILLING_FAX_NUM ,
s.BILLING_EMAIL_ADDR ,
s.SELF_REGISTRATION ,
s.SELF_REGISTRATION_DATE,
s.ACCESS_TO_ORG_FIN_ACT ,
s.NOTIFY_DEV_PLAN_ITEM_ADD ,
s.NOTIFY_DEV_PLAN_ITEM_MOD ,
s.NOTIFY_DEV_PLAN_ITEM_REMOVE ,
s.NOTIFY_WHEN_SUB_ITEM_COMPLETE ,
s.NOTIFY_WHEN_SUB_ITEM_FAILURE ,
s.LOCKED ,
s.PASSWORD_EXP_DATE,
s.SECURITY_QUESTION ,
s.SECURITY_ANSWER ,
s.ROLE_ID ,
s.IMAGE_ID ,
s.GENDER ,
s.PAST_SERVICE,
s.LST_UNLOCK_TSTMP,
s.LST_UPD_USR,
s.LST_UPD_TSTMP,
s.MANAGE_SUB_SP,
s.MANAGE_OWN_SP,
e.cpnt_typ_id AS cpntTypeID,
e.cpnt_id AS cpntID,
e.rev_dte AS revDate,
e.rev_num AS revNum,
e.schd_id AS schedID,
cp.cpnt_title AS cpntDesc,
e.grade,
e.compl_dte AS complDate,
e.cmpl_stat_id AS complStatID,
cs.cmpl_stat_desc AS complStatDesc,
NVL(e.total_hrs, 0) AS totalHrs,
NVL(e.credit_hrs, 0) AS creditHrs,
NVL(e.contact_hrs, 0) AS contactHrs,
NVL(e.cpe_hrs, 0) AS cpeHrs,
NVL(oi.price, 0) AS tuition,
e.inst_name AS instName,
e.comments AS eventComments,
e.es_user_name AS esUserName,
e.lst_upd_tstmp AS lstTmsp,
e.esig_meaning_code_id
|| ' '
|| e.esig_meaning_code_desc AS esigMeaningCode,
e.currency_code AS currencyCode,
cur.symbol AS currencySymbol,
cur.description,
cur.active,
cur.is_default,
cur.label_id ,
s.jl_id ,
s.job_title ,
s.super ,
s.hire_dte ,
s.term_dte ,
eeno.user_value ,
co.user_value,
e.schd_id,
sc.FACILITY_ID
FROM pa_student s,
PA_CPNT_EVTHST e,
PA_CMPL_STAT cs,
pa_fin_order_item oi,
pa_currency cur,
pa_cpnt cp,
(SELECT s.stud_id,
f.user_value
FROM PA_STUD_USER f,
pa_student s
WHERE s.stud_id = f.stud_id (+)
AND f.col_num (+) = '10'
) eeno ,
(SELECT s.stud_id,
f.user_value
FROM PA_STUD_USER f,
pa_student s
WHERE s.stud_id = f.stud_id (+)
AND f.col_num (+) = '40'
) co ,
(select sc.FACILITY_ID, ce.schd_id from PA_SCHED sc
where sc.SCHD_ID (+) = e.schd_id
) sc
WHERE e.stud_id = s.stud_id
AND e.cmpl_stat_id = cs.cmpl_stat_id
AND e.currency_code = cur.currency_code(+)
AND e.order_item_id = oi.order_item_id(+)
AND cp.cpnt_typ_id(+) = e.cpnt_typ_id
AND cp.cpnt_id(+) = e.cpnt_id
AND cp.rev_dte(+) = e.rev_dte
AND s.stud_id = eeno.stud_id(+)
AND s.stud_id = co.stud_id(+);

Tuning SQL Query in Oracle

I would really appreciate your help in tuning the below SQL query. It kept running for 10 mins when I cancelled it.
MARC_SEL gave me 31,253 records in 52 seconds
and MVKE_SEL gave me 431,060 records in 22 seconds
I refactored it to use with clause but nothing much changed.What else can I incorporate to make it faster. Please help.
WITH ALL_XSAP_MATNR
AS (SELECT DISTINCT XSAP.MATNR,XSAP.MTART,XSAP.SOURCE FROM XXX_MAIN.XXX_XSAP XSAP
WHERE SOURCE = 'SP' )
, MARC_SEL AS
( SELECT DISTINCT A.SOURCE
,MARA.MATNR
,MARA.MTART
,MARA.MBRSH
,MARC.WERKS
,NVL(PX.WERKS,'/') DWERK
,NVL(MBEW.HKMAT,'/') HKMAT
,NVL(MBEW.EKALR,'/') EKALR
,NVL(MARC.STAWN,'/') STAWN
FROM ALL_XSAP_MATNR A
, XXX_MAIN.XXX_SAP_MARA MARA
, XXX_MAIN.XXX_SAP_MARC MARC
, XXX_MAIN.XXX_MP_WERKS_PLANT_XREF PX
, XXX_MAIN.XXX_SAP_MBEW MBEW
WHERE A.MATNR = MARA.MATNR
AND A.MTART = MARA.MTART
AND MARA.MATNR = MARC.MATNR
AND MARC.MATNR = MBEW.MATNR
AND MARC.WERKS = MBEW.BWKEY
AND PX.LEGACY_PLANT = MARC.WERKS
AND PX.SOURCE = 'SP'
)
, MVKE_SEL AS
( SELECT DISTINCT
MVKE.MATNR
,'/' LEGACY_ORG
,'/' LEGACY_MATNR
,NVL(MX_VKORG.SAP_DE_VAL,'/') VKORG
,NVL(SUBSTR(MX_VKORG.SAP_DE,6,2),'/') VTWEG
-- ,NVL(TVRKME.MSEH3,'/') VRKME
,NVL(MVKE.KONDM,'/') KONDM
,NVL(MVKE.VERSG,'/') VERSG
,'/' IPRKZ
,'/' MHDRZ,NVL(MVKE.VMSTA,'/') VMSTA
,NVL(TO_CHAR(MVKE.VMSTD ,'YYYYMMDD' ),'/') VMSTD
,NVL(MVKE.PMATN,'/') PMATN
,NVL(MVKE.MVGR2,'/') MVGR2
,NVL(MVKE.MVGR3,'/') MVGR3
,NVL(MVKE.VAVME,'/') VAVME
,'/' MVGR4
,'/' MVGR5
,NVL(MVKE.MTPOS,'/') MTPOS
,NVL(MVKE.PRAT1,'/') PRAT1
,NVL(MVKE.SKTOF,'/') SKTOF
,'/' AUMNG
,NVL(MVKE.PRODH,'/') PRODH
,'/' MVGR1
,NVL(MVKE.KTGRM,'/') KTGRM
,MX_VKORG.DESC4
FROM XXX_MAIN.XXX_SAP_MVKE MVKE
, XXX_MAIN.XXX_MP_VKVT_XREF MX_VKORG
WHERE MX_VKORG.SOURCE_DE_VAL = MVKE.VKORG
AND SUBSTR(MX_VKORG.SAP_DE,6,2) = MVKE.VTWEG
AND MX_VKORG.SOURCE_TBL = 'SP'
AND MX_VKORG.SOURCE_DE = 'MVKE'
AND SUBSTR(MX_VKORG.SAP_DE,1,5)= 'VKORG'
AND MX_VKORG.DESC2 IS NULL )
SELECT DISTINCT
MARC.SOURCE
,MARC.MATNR
,MARC.MTART
,MARC.MBRSH
,MARC.WERKS
,MARC.DWERK
,MARC.HKMAT
,MARC.EKALR
,MARC.STAWN
,MVKE.LEGACY_ORG
,MVKE.LEGACY_MATNR
,MVKE.VKORG
,MVKE.VTWEG
,MVKE.KONDM
,MVKE.VERSG
,MVKE.VMSTA
,MVKE.VMSTD
,MVKE.PMATN
,MVKE.MVGR2
,MVKE.MVGR3
,MVKE.VAVME
,MVKE.MTPOS
,MVKE.PRAT1
,MVKE.SKTOF
,MVKE.PRODH
,MVKE.KTGRM
FROM MARC_SEL MARC
, MVKE_SEL MVKE
WHERE MARC.MATNR = MVKE.MATNR
AND MARC.WERKS = MVKE.DESC4
Start by simplifying your query as you do not need to do multiple DISTINCTs (it's only necessary in the final output) and you are selecting many columns that you are not outputting. You are also joining some tables which you are not selecting from and if there are multiple matching rows for these then it may generate duplicate rows - using something like EXISTS can eliminate these joins.
Like this:
WITH MARC_SEL AS (
SELECT A.SOURCE,
MARA.MATNR,
MARC.WERKS
FROM XXX_MAIN.XXX_XSAP A
INNER JOIN XXX_MAIN.XXX_SAP_MARA MARA
ON ( A.MATNR = MARA.MATNR
AND A.MTART = MARA.MTART )
INNER JOIN XXX_MAIN.XXX_SAP_MARC MARC
ON ( MARA.MATNR = MARC.MATNR )
WHERE EXISTS( SELECT 'X'
FROM XXX_MAIN.XXX_MP_WERKS_PLANT_XREF PX
WHERE PX.LEGACY_PLANT = MARC.WERKS
AND PX.SOURCE = 'SP' )
AND EXISTS( SELECT 'X'
FROM XXX_MAIN.XXX_SAP_MBEW MBEW
WHERE MARC.MATNR = MBEW.MATNR
AND MARC.WERKS = MBEW.BWKEY )
AND A.SOURCE = 'SP'
)
, MVKE_SEL AS (
SELECT NVL(MX_VKORG.SAP_DE_VAL,'/') VKORG,
NVL(SUBSTR(MX_VKORG.SAP_DE,6,2),'/') VTWEG,
MX_VKORG.DESC4
FROM XXX_MAIN.XXX_MP_VKVT_XREF MX_VKORG
WHERE EXISTS ( SELECT 'X'
FROM XXX_MAIN.XXX_SAP_MVKE MVKE
WHERE MX_VKORG.SOURCE_DE_VAL = MVKE.VKORG
AND SUBSTR(MX_VKORG.SAP_DE,6,2) = MVKE.VTWEG )
AND MX_VKORG.SOURCE_TBL = 'SP'
AND MX_VKORG.SOURCE_DE = 'MVKE'
AND SUBSTR(MX_VKORG.SAP_DE,1,5)= 'VKORG'
AND MX_VKORG.DESC2 IS NULL
)
SELECT DISTINCT
MARC.SOURCE,
MARC.MATNR,
MVKE.VKORG,
MARC.WERKS,
MVKE.VTWEG
FROM MARC_SEL MARC
INNER JOIN MVKE_SEL MVKE
ON ( MARC.MATNR = MVKE.MATNR
AND MARC.WERKS = MVKE.DESC4 )
Added hints to subqueries and it came back in a minute.
WITH ALL_XSAP_MATNR
AS (SELECT /*+ materialize */ DISTINCT XSAP.MATNR,XSAP.MTART,XSAP.SOURCE FROM XXX_MAIN.XXX_XSAP XSAP
WHERE SOURCE = 'SP' )
, MARC_SEL AS
( SELECT /*+ materialize */ DISTINCT A.SOURCE
,MARA.MATNR
,MARA.MTART
,MARA.MBRSH
,MARC.WERKS
,NVL(PX.WERKS,'/') DWERK
,NVL(MBEW.HKMAT,'/') HKMAT
,NVL(MBEW.EKALR,'/') EKALR
,NVL(MARC.STAWN,'/') STAWN
FROM ALL_XSAP_MATNR A
, XXX_MAIN.XXX_SAP_MARA MARA
, XXX_MAIN.XXX_SAP_MARC MARC
, XXX_MAIN.XXX_MP_WERKS_PLANT_XREF PX
, XXX_MAIN.XXX_SAP_MBEW MBEW
WHERE A.MATNR = MARA.MATNR
AND A.MTART = MARA.MTART
AND MARA.MATNR = MARC.MATNR
AND MARC.MATNR = MBEW.MATNR
AND MARC.WERKS = MBEW.BWKEY
AND PX.LEGACY_PLANT = MARC.WERKS
AND PX.SOURCE = 'SP'
)
, MVKE_SEL AS
( SELECT /*+ materialize */ DISTINCT
MVKE.MATNR
,'/' LEGACY_ORG
,'/' LEGACY_MATNR
,NVL(MX_VKORG.SAP_DE_VAL,'/') VKORG
,NVL(SUBSTR(MX_VKORG.SAP_DE,6,2),'/') VTWEG
-- ,NVL(TVRKME.MSEH3,'/') VRKME
,NVL(MVKE.KONDM,'/') KONDM
,NVL(MVKE.VERSG,'/') VERSG
,'/' IPRKZ
,'/' MHDRZ,NVL(MVKE.VMSTA,'/') VMSTA
,NVL(TO_CHAR(MVKE.VMSTD ,'YYYYMMDD' ),'/') VMSTD
,NVL(MVKE.PMATN,'/') PMATN
,NVL(MVKE.MVGR2,'/') MVGR2
,NVL(MVKE.MVGR3,'/') MVGR3
,NVL(MVKE.VAVME,'/') VAVME
,'/' MVGR4
,'/' MVGR5
,NVL(MVKE.MTPOS,'/') MTPOS
,NVL(MVKE.PRAT1,'/') PRAT1
,NVL(MVKE.SKTOF,'/') SKTOF
,'/' AUMNG
,NVL(MVKE.PRODH,'/') PRODH
,'/' MVGR1
,NVL(MVKE.KTGRM,'/') KTGRM
,MX_VKORG.DESC4
FROM XXX_MAIN.XXX_SAP_MVKE MVKE
, XXX_MAIN.XXX_MP_VKVT_XREF MX_VKORG
WHERE MX_VKORG.SOURCE_DE_VAL = MVKE.VKORG
AND SUBSTR(MX_VKORG.SAP_DE,6,2) = MVKE.VTWEG
AND MX_VKORG.SOURCE_TBL = 'SP'
AND MX_VKORG.SOURCE_DE = 'MVKE'
AND SUBSTR(MX_VKORG.SAP_DE,1,5)= 'VKORG'
AND MX_VKORG.DESC2 IS NULL )
SELECT DISTINCT /*+ use_hash(MARC,MVKE ) */
MARC.SOURCE
,MARC.MATNR
,MARC.MTART
,MARC.MBRSH
,MARC.WERKS
,MARC.DWERK
,MARC.HKMAT
,MARC.EKALR
,MARC.STAWN
,MVKE.LEGACY_ORG
,MVKE.LEGACY_MATNR
,MVKE.VKORG
,MVKE.VTWEG
,MVKE.KONDM
,MVKE.VERSG
,MVKE.VMSTA
,MVKE.VMSTD
,MVKE.PMATN
,MVKE.MVGR2
,MVKE.MVGR3
,MVKE.VAVME
,MVKE.MTPOS
,MVKE.PRAT1
,MVKE.SKTOF
,MVKE.PRODH
,MVKE.KTGRM
FROM MARC_SEL MARC
, MVKE_SEL MVKE
WHERE MARC.MATNR = MVKE.MATNR
AND MARC.WERKS = MVKE.DESC4

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 `

SQL: not exist with openquery not working as expected

I do have an oracle 8 database from which I want to fetch data to SQL Server 2005. The following statement works fine, if the table on SQL Server 2005 is empty. If I run with let's say one entry missing, it does not work. Please let me know, if any additional information would be usefull!
SELECT wdmsoracle.NO
FROM (Select * from OPENQUERY(msch, 'select
actu.NO
, actu.ANNOSEQUENT
, replace(replace(dm.descript, ''Generated by modification request '', ''Modification request''), ''Pseudo ECR for WDMS-SAP interface'', ''New Drawing'')
, actu.APPRDATE
, actu.MADEDATE
, actu.MAINDRW
, actu.DESIGNGRP
, actu.ITEMID
, actu.ISSUE
, actu.DESCRIPT
, actu.DESCRIPT2
, dr.PRODUCTCODE
, mpaths.webpath as asdasd
, vpaths.webpath
FROM
WDDRAWACTU actu
, wddraw dr
, wddvfilesiss vfiles
, wddvpaths vpaths
, wddmfiles mfiles
, wddmpaths mpaths
, wdanno dm
WHERE actu.apprdate >= to_date(''01-01-05'',''dd-mm-yy'')
and actu.itemid = dr.itemid (+)
and actu.issue = dr.issue (+)
and actu.annosequent = dm.sequent (+)
and actu.itemid = vfiles.itemid (+)
and actu.issue = vfiles.issue (+)
and vfiles.pathid = vpaths.pathid
and actu.annosequent = mfiles.sequent (+)
and mfiles.pathid = mpaths.pathid')) as wdmsoracle
Where NOT EXISTS (
SELECT wdmsoracle.NO
FROM [DesignMessage_Workflow].[dbo].[WDMS] wdms, (Select * from OPENQUERY(msch, 'select
actu.NO
FROM
WDDRAWACTU actu
WHERE actu.apprdate >= to_date(''01-01-05'',''dd-mm-yy'')')) as wdmsoracle
WHERE wdms.NO = wdmsoracle.NO)
with the help of this website I was able to solve it on my own:
SELECT wdmsoracle.NO
FROM (Select * from OPENQUERY(msch, 'select
actu.NO
, actu.ANNOSEQUENT
, replace(replace(dm.descript, ''Generated by modification request '', ''Modification request''), ''Pseudo ECR for WDMS-SAP interface'', ''New Drawing'')
, actu.APPRDATE
, actu.MADEDATE
, actu.MAINDRW
, actu.DESIGNGRP
, actu.ITEMID
, actu.ISSUE
, actu.DESCRIPT
, actu.DESCRIPT2
, dr.PRODUCTCODE
, mpaths.webpath as asdasd
, vpaths.webpath
FROM
WDDRAWACTU actu
, wddraw dr
, wddvfilesiss vfiles
, wddvpaths vpaths
, wddmfiles mfiles
, wddmpaths mpaths
, wdanno dm
WHERE actu.apprdate >= to_date(''01-01-05'',''dd-mm-yy'')
and actu.itemid = dr.itemid (+)
and actu.issue = dr.issue (+)
and actu.annosequent = dm.sequent (+)
and actu.itemid = vfiles.itemid (+)
and actu.issue = vfiles.issue (+)
and vfiles.pathid = vpaths.pathid
and actu.annosequent = mfiles.sequent (+)
and mfiles.pathid = mpaths.pathid')) as wdmsoracle
Where NOT EXISTS (
SELECT wdms.NO
FROM [DesignMessage_Workflow].[dbo].[WDMS] wdms
WHERE wdms.NO = wdmsoracle.NO)