Select or Join tatement for BIRT Report using SQL - 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(+);

Related

Easier Merge statement

I am trying to write a BQ Merge Query which does something like an upsert. I have a main table(M) and an identical delta table(D) which brings changed records. The merge statement should work like if the keys match it will update all the table fields in the main table with the delta table data and if the keys don't match it will do an insert of the delta table values. I was able to get the Merge statement to work but it seems too long. Is there a shorter way to do this . I have more than 190 columns in my table. Please see the Merge code below.
merge zsac_figl01.zsaplikp02 M
using zsac_figl01.zsaplikp02_delta D
on M.VBELN = D.VBELN
WHEN MATCHED THEN
UPDATE
SET
M.VBELN = D.VBELN ,
M.ERNAM = D.ERNAM ,
M.ERZET = D.ERZET ,
M.ERDAT = D.ERDAT ,
M.BZIRK = D.BZIRK ,
M.VSTEL = D.VSTEL ,
M.VKORG = D.VKORG ,
M.LFART = D.LFART ,
M.AUTLF = D.AUTLF ,
M.KZAZU = D.KZAZU ,
M.WADAT = D.WADAT ,
M.LDDAT = D.LDDAT ,
M.TDDAT = D.TDDAT ,
M.LFDAT = D.LFDAT ,
M.KODAT = D.KODAT ,
M.ABLAD = D.ABLAD ,
M.INCO1 = D.INCO1 ,
M.INCO2 = D.INCO2 ,
M.EXPKZ = D.EXPKZ ,
M.ROUTE = D.ROUTE ,
M.FAKSK = D.FAKSK ,
M.LIFSK = D.LIFSK ,
M.VBTYP = D.VBTYP ,
M.KNFAK = D.KNFAK ,
M.TPQUA = D.TPQUA ,
M.TPGRP = D.TPGRP ,
M.LPRIO = D.LPRIO ,
M.VSBED = D.VSBED ,
M.KUNNR = D.KUNNR ,
M.KUNAG = D.KUNAG ,
M.KDGRP = D.KDGRP ,
M.STZKL = D.STZKL ,
M.STZZU = D.STZZU ,
M.BTGEW = D.BTGEW ,
M.NTGEW = D.NTGEW ,
M.GEWEI = D.GEWEI ,
M.VOLUM = D.VOLUM ,
M.VOLEH = D.VOLEH ,
M.ANZPK = D.ANZPK ,
M.BEROT = D.BEROT ,
M.LFUHR = D.LFUHR ,
M.GRULG = D.GRULG ,
M.LSTEL = D.LSTEL ,
M.TRAGR = D.TRAGR ,
M.FKARV = D.FKARV ,
M.FKDAT = D.FKDAT ,
M.PERFK = D.PERFK ,
M.ROUTA = D.ROUTA ,
M.STAFO = D.STAFO ,
M.KALSM = D.KALSM ,
M.KNUMV = D.KNUMV ,
M.WAERK = D.WAERK ,
M.VKBUR = D.VKBUR ,
M.VBEAK = D.VBEAK ,
WHEN NOT MATCHED THEN INSERT
(
VBELN ,
ERNAM ,
ERZET ,
ERDAT ,
BZIRK ,
VSTEL ,
VKORG ,
LFART ,
AUTLF ,
KZAZU ,
WADAT ,
LDDAT ,
TDDAT ,
LFDAT ,
KODAT ,
ABLAD ,
INCO1 ,
INCO2 ,
EXPKZ ,
ROUTE ,
FAKSK ,
LIFSK ,
VBTYP ,
KNFAK ,
TPQUA ,
TPGRP ,
LPRIO ,
VSBED ,
KUNNR ,
KUNAG ,
KDGRP ,
STZKL ,
STZZU ,
BTGEW ,
NTGEW ,
GEWEI ,
VOLUM ,
VOLEH ,
ANZPK ,
BEROT ,
LFUHR ,
GRULG ,
LSTEL ,
TRAGR ,
FKARV ,
FKDAT ,
PERFK ,
ROUTA ,
STAFO ,
KALSM ,
KNUMV ,
WAERK ,
VKBUR ,
VBEAK ,
)
VALUES
(
D.VBELN,
D.ERNAM,
D.ERZET,
D.ERDAT,
D.BZIRK,
D.VSTEL,
D.VKORG,
D.LFART,
D.AUTLF,
D.KZAZU,
D.WADAT,
D.LDDAT,
D.TDDAT,
D.LFDAT,
D.KODAT,
D.ABLAD,
D.INCO1,
D.INCO2,
D.EXPKZ,
D.ROUTE,
D.FAKSK,
D.LIFSK,
D.VBTYP,
D.KNFAK,
D.TPQUA,
D.TPGRP,
D.LPRIO,
D.VSBED,
D.KUNNR,
D.KUNAG,
D.KDGRP,
D.STZKL,
D.STZZU,
D.BTGEW,
D.NTGEW,
D.GEWEI,
D.VOLUM,
D.VOLEH,
D.ANZPK,
D.BEROT,
D.LFUHR,
D.GRULG,
D.LSTEL,
D.TRAGR,
D.FKARV,
D.FKDAT,
D.PERFK,
D.ROUTA,
D.STAFO,
D.KALSM,
D.KNUMV,
D.WAERK,
D.VKBUR,
D.VBEAK,
D.ZUKRL,
D.VERUR,
D.COMMN,
D.STWAE,
D.STCUR,
D.EXNUM,
D.AENAM,
D.AEDAT,
D.LGNUM,
D.LISPL,
D.VKOIV,
D.VTWIV,
D.SPAIV,
D.FKAIV,
D.PIOIV,
D.FKDIV,
D.KUNIV,
D.KKBER,
D.KNKLI,
D.GRUPP,
D.SBGRP,
D.CTLPC,
D.CMWAE,
D.AMTBL,
D.BOLNR,
D.LIFNR,
D.TRATY,
D.TRAID,
D.CMFRE,
D.CMNGV,
D.XABLN,
D.BLDAT,
D.WADAT_IST,
D.TRSPG,
D.TPSID,
D.LIFEX,
D.TERNR,
D.KALSM_CH,
D.KLIEF,
D.KALSP,
D.KNUMP,
D.NETWR,
D.AULWE,
D.WERKS,
D.LCNUM,
D.ABSSC,
D.KOUHR,
D.TDUHR,
D.LDUHR,
D.WAUHR,
D.LGTOR,
D.LGBZO,
D.AKWAE,
D.AKKUR,
D.AKPRZ,
D.PROLI,
D.XBLNR,
D.HANDLE,
D.TSEGFL,
D.TSEGTP,
D.TZONIS,
D.TZONRC,
D.CONT_DG,
D.VERURSYS,
D.KZWAB,
D.VLSTK,
D.TCODE,
D.VSART,
D.TRMTYP,
D.SDABW,
D.VBUND,
D.XWOFF,
D.DIRTA,
D.PRVBE,
D.FOLAR,
D.PODAT,
D.POTIM,
D.VGANZ,
D.IMWRK,
D.SPE_LOEKZ,
D.SPE_LOC_SEQ,
D.SPE_ACC_APP_STS,
D.SPE_SHP_INF_STS,
D.SPE_RET_CANC,
D.SPE_WAUHR_IST,
D.SPE_WAZONE_IST,
D.SPE_REV_VLSTK,
D.SPE_LE_SCENARIO,
D.SPE_ORIG_SYS,
D.SPE_CHNG_SYS,
D.SPE_GEOROUTE,
D.SPE_GEOROUTEIND,
D.SPE_CARRIER_IND,
D.SPE_GTS_REL,
D.SPE_GTS_RT_CDE,
D.SPE_REL_TMSTMP,
D.SPE_UNIT_SYSTEM,
D.SPE_INV_BFR_GI,
D.SPE_QI_STATUS,
D.SPE_RED_IND,
D.SAKES,
D.SPE_LIFEX_TYPE,
D.SPE_TTYPE,
D.SPE_PRO_NUMBER,
D.LOC_GUID,
D.SPE_BILLING_IND,
D.PRINTER_PROFILE,
D.MSR_ACTIVE,
D.PRTNR,
D.STGE_LOC_CHANGE,
D.TM_CTRL_KEY,
D.DLV_SPLIT_INITIA,
D.DLV_VERSION,
D.BEV1_LULEINH,
D.BEV1_RPFAESS,
D.BEV1_RPKIST,
D.BEV1_RPCONT,
D.BEV1_RPSONST,
D.BEV1_RPFLGNR,
D.BORGR_GRP,
D.ZLAND1,
D.ZZTVGW,
D.ZZVTW,
D.ZZFRGEW,
D.ZZANZPAL,
D.ZZKDKG1,
D.ZZVOLLPAL
)`
if the keys match it will update all the table fields in the main table with the delta table data and if the keys don't match it will do an insert of the delta table values
Based on above I assume the schema of main and delta table are identical
So, you can explore option of doing DELETE and INSERT instead of UPDATE and INSERT
Something like
DELETE zsac_figl01.zsaplikp02
WHERE VBELN IN (SELECT VBELN FROM zsac_figl01.zsaplikp02_delta)
And then (note - Omitting column names is in Beta since June 3, 2019, so you should try )
INSERT zsac_figl01.zsaplikp02
SELECT * FROM zsac_figl01.zsaplikp02_delta

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

execute sql query in zend framework 2

I need execute this SQL query in ZF2:
SELECT FA_VENTAS.EM_CODIGO,
FA_VENDED.VN_NOMBRE AS VENDEDOR ,
FA_CLIEN.CE_NOMBRE AS CLIENTE,
FA_TIPCLI.TC_DESCRI AS CANAL ,
MIN(FA_DETVEN.CL_NOMDEP) AS MARCA ,
IN_ITEM.IT_CODBAR AS CODBARRA ,
IN_ITEM.IT_NOMBRE AS PRODUCTO ,
TO_CHAR(FA_VENTAS.VE_FECHA,'MM')AS MESES ,
TO_CHAR(FA_VENTAS.VE_FECHA,'YYYY')AS ANIO ,
SUM(DV_SUBTOTAL-DV_VALIVA) AS VALOR ,
SUM(DV_CANPED) AS UNIDAD
FROM
DOUS.FA_VENTAS FA_VENTAS,
DOUS.FA_DETVEN FA_DETVEN,
DOUS.IN_ITEM IN_ITEM ,
DOUS.IN_CLASE IN_CLASE ,
DOUS.FA_VENDED FA_VENDED,
DOUS.FA_CLIEN FA_CLIEN,
DOUS.FA_TIPCLI FA_TIPCLI
WHERE
FA_TIPCLI.TC_CODIGO=FA_CLIEN.TC_CODIGO
AND FA_VENTAS.EM_CODIGO = FA_DETVEN.EM_CODIGO
AND FA_VENTAS.VE_CODIGO = FA_DETVEN.VE_CODIGO
AND FA_VENTAS.TP_CODIGO = FA_DETVEN.TP_CODIGO
AND FA_VENTAS.PE_CODIGO = FA_DETVEN.PE_CODIGO
AND FA_DETVEN.EM_CODIGO = IN_ITEM.EM_CODIGO
AND FA_DETVEN.IT_CODIGO = IN_ITEM.IT_CODIGO
AND IN_ITEM.EM_CODIGO = IN_CLASE.EM_CODIGO
AND IN_ITEM.CL_CODIGO = IN_CLASE.CL_CODIGO
AND FA_VENDED.VN_CODIGO=4
AND FA_VENTAS.CE_CODIGO=FA_CLIEN.CE_CODIGO
AND FA_VENTAS.EM_CODIGO = '4'
AND FA_VENTAS.TP_CODIGO = '2'
AND TRUNC(FA_VENTAS.VE_FECHA) between'01-jan-2009' and '31-jan-2009'
GROUP BY FA_VENTAS.EM_CODIGO,
FA_VENDED.VN_NOMBRE,
FA_CLIEN.CE_NOMBRE,
FA_DETVEN.CL_NOMDEP,
FA_VENTAS.VE_FECHA,
IN_ITEM.IT_CODBAR,
IN_ITEM.IT_NOMBRE,
FA_TIPCLI.TC_DESCRI
UNION ALL
SELECT FA_DEVOLU.EM_CODIGO,
FA_VENDED.VN_NOMBRE AS VENDEDOR ,
FA_CLIEN.CE_NOMBRE AS CLIENTE,
FA_TIPCLI.TC_DESCRI AS CANAL ,
MIN(FA_DETDEVOL.CL_NOMDEP)AS MARCA ,
IN_ITEM.IT_CODBAR AS CODBARRA ,
IN_ITEM.IT_NOMBRE AS PRODUCTO ,
TO_CHAR(FA_DEVOLU.DE_FECHA,'MM') AS MESES ,
TO_CHAR(FA_DEVOLU.DE_FECHA,'YYYY') AS ANIO ,
-SUM(FA_DETDEVOL.DD_SUBTOTAL- FA_DETDEVOL.DD_VALIVA) AS VALOR ,
-SUM(DD_CANTID) AS UNIDAD
FROM
DOUS.FA_DEVOLU,
DOUS.FA_DETDEVOL,
DOUS.IN_ITEM IN_ITEM ,
DOUS.IN_CLASE IN_CLASE ,
DOUS.FA_VENDED FA_VENDED,
DOUS.FA_CLIEN FA_CLIEN,
DOUS.FA_TIPCLI FA_TIPCLI
WHERE
FA_TIPCLI.TC_CODIGO=FA_CLIEN.TC_CODIGO
AND FA_DEVOLU.EM_CODIGO = FA_DETDEVOL.EM_CODIGO
AND FA_DEVOLU.DE_CODIGO = FA_DETDEVOL.DE_CODIGO
AND FA_DEVOLU.TP_CODIGO = FA_DETDEVOL.TP_CODIGO
AND FA_DETDEVOL.EM_CODIGO = IN_ITEM.EM_CODIGO
AND FA_DETDEVOL.IT_CODIGO = IN_ITEM.IT_CODIGO
AND IN_ITEM.EM_CODIGO = IN_CLASE.EM_CODIGO
AND IN_ITEM.CL_CODIGO = IN_CLASE.CL_CODIGO
AND FA_DETDEVOL.EM_CODIGO = 4
AND FA_DETDEVOL.TP_CODIGO = '2'
AND TRUNC(FA_DEVOLU.DE_FECHA) between '01-jan-2009' and '31-jan-2009'
AND FA_VENDED.VN_CODIGO=4
AND FA_DEVOLU.CE_CODIGO=FA_CLIEN.CE_CODIGO
GROUP BY FA_DEVOLU.EM_CODIGO,
FA_VENDED.VN_NOMBRE,
FA_CLIEN.CE_NOMBRE,
FA_DETDEVOL.CL_NOMDEP,
FA_DEVOLU.DE_FECHA,
IN_ITEM.IT_CODBAR,
IN_ITEM.IT_NOMBRE,
FA_TIPCLI.TC_DESCRI
but I don't understand what model, view and command use.
Please help me
With a query like this, you should use \Zend\Db\Adapter\Adapter
$sql = "SELECT SELECT FA_VENTAS.EM_CODIGO, ...";
$statement = $adapter->query($sql);
$result = $statement->execute();
hth

SubQuery added to existing Query in PL/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.

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)