Any alternate of REGEXP_LIKE as it causing performance issue - sql

We have one sql which is performing bad.I have fixed the execution plan but REGEXP_LIKE causing high buffer gets and also contribute in runtime.Any alternate which i can use in my sql.The problematc is
AND REGEXP_LIKE (xx3la.assembly_type, '^I..*')
/* Formatted on 1/6/2023 2:07:28 PM (QP5 v5.318) */
SELECT 1
FROM apps.oe_order_lines_all ola
JOIN APPS.OE_order_headers_all oh
ON oh.Header_Id = ola.header_id AND oh.org_id = ola.org_id
JOIN inv.mtl_parameters org
ON ola.ship_from_org_id = org.ORGANIZATION_ID
INNER JOIN ont.oe_transaction_types_tl tt
ON tt.transaction_type_id = oh.order_type_id
AND tt.language = 'US'
AND LPAD (UPPER (tt.name), 4) NOT IN ('TRIA',
'WARR',
'REPA',
'DUMM',
'DEMO',
'GOOD',
'INTE',
'CRED',
'EVAL',
'INVO') --- ('STAND')
JOIN apps.mtl_system_items_b msib
ON ola.Ordered_item_Id = msib.inventory_item_id
AND msib.ORGANIZATION_ID = org.ORGANIZATION_ID
INNER JOIN xxom.xxom_3lp_sym_ora_order_hdr xx3ha
ON xx3ha.header_id = oh.header_id
INNER JOIN xxom.xxom_3lp_sym_ora_order_lines xx3la
ON xx3la.header_id = ola.header_id AND xx3la.line_id = ola.line_id
LEFT JOIN
(SELECT msib.Inventory_item_id,
mcb.segment1 ProductionLine,
msib.ORGANIZATION_ID,
msib.item_type,
msib.Fixed_LEAD_Time
FROM apps.mtl_system_items_b msib
JOIN inv.mtl_parameters org
ON msib.ORGANIZATION_ID = org.ORGANIZATION_ID
JOIN apps.MTL_ITEM_CATEGORIES ic
ON ic.INVENTORY_ITEM_ID = msib.INVENTORY_ITEM_ID
AND msib.organization_id = ic.ORGANIZATION_ID
JOIN apps.mtl_categories_b mcb
ON mcb.CATEGORY_ID = ic.CATEGORY_ID
WHERE mcb.ENABLED_FLAG = 'Y' AND ic.CATEGORY_SET_ID = 1100009407) im
ON ola.Ordered_item_Id = im.inventory_item_id
AND org.organization_id = im.organization_id
LEFT JOIN ( SELECT Header_Id,
Line_Id,
WIP_ORDER_NUMBER,
MAX (PRODUCT_LINE) PRODUCT_LINE
FROM XXRMT.XXURD_SO_UNIT
GROUP BY Header_Id, Line_Id, WIP_ORDER_NUMBER) u
ON u.Header_Id = oh.Header_Id AND u.Line_Id = ola.Line_Id
LEFT JOIN wip.wip_discrete_jobs wdj
ON wdj.source_line_id = ola.line_id
AND wdj.status_type IN (1,
2,
3,
4,
5,
12)
LEFT JOIN wip.wip_entities we ON we.wip_entity_id = wdj.wip_entity_id
LEFT JOIN xxrmt.xxont_som_scheduler adm
ON adm.subscriber_id = xx3la.order_admin
LEFT JOIN apps.oe_order_lines_all ol2
ON ol2.ato_line_id = ola.line_id
AND ol2.item_type_code IN ('CONFIG')
LEFT JOIN
(SELECT mso.source_organization_code,
mso.vendor_name,
mso.vendor_site,
msa.sourcing_rule_name,
DECODE (mso.source_type,
1, 'TRANSFER',
2, 'MAKE',
3, 'BUY')
SourceType,
msa.inventory_item_id,
msa.organization_id
FROM apps.mrp_sr_assignments_v msa
--join inv.mtl_parameters org on msa.organization_id = org.organization_id
INNER JOIN apps.mrp_sourcing_rules msr
ON msr.sourcing_rule_id = msa.sourcing_rule_id
INNER JOIN apps.mrp_sr_receipt_org_v msro
ON msr.sourcing_rule_id = msro.sourcing_rule_id
INNER JOIN apps.mrp_sr_source_org_v mso
ON mso.sr_receipt_id = msro.sr_receipt_id AND mso.RANK = 1
WHERE msa.assignment_set_id = 561
AND (msro.disable_date IS NULL OR disable_date >= SYSDATE))
srd
ON srd.inventory_item_id = ol2.inventory_item_id
AND srd.organization_id = ola.ship_from_org_id
WHERE org.organization_code IN ('DRM')
AND ola.Flow_Status_Code <> 'CANCELLED'
AND NVL (oh.Cancelled_flag, 'N') = 'N'
AND oh.Flow_Status_Code <> 'CANCELLED'
AND ola.booked_flag = 'Y'
AND oh.booked_date IS NOT NULL
AND TRUNC (oh.booked_date) >= TO_DATE ('2022-12-01', 'YYYY-MM-DD')
AND msib.item_type NOT IN ('P',
'B/R',
'OP',
'SVC',
'OP',
'EMR_PURCH')
AND ( ola.item_type_code IN ('CONFIG', 'MODEL')
OR ( ola.item_type_code IN ('CLASS')
AND REGEXP_LIKE (xx3la.assembly_type, '^I..*'))
OR ( ola.item_type_code IN ('CLASS')
AND u.PRODUCT_LINE IS NOT NULL
AND xx3la.model_string IS NOT NULL
AND msib.segment1 NOT IN ('R-CAP1199', 'R-38-315'))
OR ( ola.item_type_code = 'STANDARD'
AND xx3la.assembly_type IS NULL))
Somehow fixed the BG issue but still REGEXP_LIKE an issue

Your current regex assertion:
REGEXP_LIKE(xx3la.assembly_type, '^I..*')
can be written using an ordinary LIKE:
xx3la.assembly_type LIKE 'I_%'
Note that the above LIKE version should be able to use an index on assembly_type. That being said, I doubt that the REGEXP_LIKE call be the only major performance bottleneck existing in a query of this size and complexity. You should run EXPLAIN on your query to view the execution plan.

Related

Left outer join invalid identifier

I can't seem to get these left outer joins right using derived tables. I need to do it this way as I need to use a subquery in the qualification.
The error I am receiving is ORA-00904: "E"."ENCNTR_ID": invalid identifier
I've tried moving the position of the join around and reversing the columns around the =. There's plenty of posts out there related to this but none of them have provided me the answer. Many are in regards to having multiple tables comma delimited in the FROM clause but my query doesn't have that.
SELECT
E.ENCNTR_ID
,E.REASON_FOR_VISIT
-- ,D.DIAGNOSIS_DISPLAY AS DISCHARGE_DX
-- ,D.DIAG_FTDESC AS WORKING_DX
,cclsql_cnvtdatetimeutc(E.REG_DT_TM, 2, 126) as reg_dt_tm
,cclsql_cnvtdatetimeutc(E.DISCH_DT_TM, 2, 126) as disch_dt_tm
,FACILITY.DISPLAY AS FACILITY
,P.PERSON_ID
,P.NAME_FULL_FORMATTED
,cclsql_cnvtdatetimeutc(P.BIRTH_DT_TM, 2, P.BIRTH_TZ) as birth_dt_tm
,MRN.ALIAS AS MRN
,FIN.ALIAS AS FIN
,ENC_TYPE.DISPLAY AS ENCOUNTER_TYPE
,NURSE_UNIT.DISPLAY AS NURSE_UNIT
,ROOM.DISPLAY AS ROOM
,BED.DISPLAY AS BED
,ADMITDOC_NAME.NAME_FULL_FORMATTED AS ADMITDOC_NAME
,DCDOC_NAME.NAME_FULL_FORMATTED AS DCDOC_NAME
,ADMIT_SRC.DISPLAY AS ADMIT_SOURCE
,ADMIT_TYPE.DISPLAY AS ADMIT_TYPE
,ESDP.RESULT_VAL AS ESDP
,DISCH_DISPOSITION.DISPLAY AS DISCH_DISPOSITION
FROM ENCOUNTER E
-- JOIN DISCHARGE D ON (D.ENCNTR_ID = E.ENCNTR_ID)
-- AND D.ACTIVE_IND = 1
-- AND D.END_EFFECTIVE_DT_TM > SYSDATE
JOIN PERSON P ON (P.PERSON_ID = E.PERSON_ID)
JOIN CODE_VALUE ENC_TYPE ON (ENC_TYPE.CODE_VALUE = E.ENCNTR_TYPE_CLASS_CD)
JOIN ENCNTR_ALIAS FIN ON (FIN.ENCNTR_ID = E.ENCNTR_ID)
AND FIN.ENCNTR_ALIAS_TYPE_CD = 1077 -- FIN CODE
AND FIN.ACTIVE_IND = 1
AND FIN.ACTIVE_STATUS_CD = 188
JOIN ENCNTR_ALIAS MRN ON (MRN.ENCNTR_ID = E.ENCNTR_ID)
AND MRN.ENCNTR_ALIAS_TYPE_CD = 1079 -- MRN CODE
AND MRN.ACTIVE_IND = 1
AND MRN.ACTIVE_STATUS_CD = 188
JOIN CODE_VALUE FACILITY on (FACILITY.CODE_VALUE = E.LOC_FACILITY_CD)
JOIN CODE_VALUE NURSE_UNIT on (NURSE_UNIT.CODE_VALUE = E.LOC_NURSE_UNIT_CD)
JOIN CODE_VALUE ROOM on (ROOM.CODE_VALUE = E.LOC_ROOM_CD)
JOIN CODE_VALUE BED on (BED.CODE_VALUE = E.LOC_BED_CD)
JOIN CODE_VALUE DISCH_DISPOSITION on (DISCH_DISPOSITION.CODE_VALUE = E.DISCH_DISPOSITION_CD)
JOIN PRSNL ADMITDOC_NAME ON (ADMITDOC_NAME.PERSON_ID = OA.ORDER_PROVIDER_ID)
LEFT JOIN CLINICAL_EVENT HOSP_SUM ON (HOSP_SUM.ENCNTR_ID = E.ENCNTR_ID)
AND HOSP_SUM.EVENT_CD = :p17
AND HOSP_SUM.RESULT_STATUS_CD IN (:p19, :p20, :p21)
AND HOSP_SUM.RECORD_STATUS_CD = 188 -- ACTIVE
AND HOSP_SUM.VALID_UNTIL_DT_TM > cclsql_cnvtdatetimeutc(SYSDATE, 1, 126)
LEFT JOIN PRSNL DCDOC_NAME ON (DCDOC_NAME.PERSON_ID = HOSP_SUM.VERIFIED_PRSNL_ID)
JOIN CODE_VALUE ADMIT_SRC ON (ADMIT_SRC.CODE_VALUE = E.ADMIT_SRC_CD)
JOIN CODE_VALUE ADMIT_TYPE ON (ADMIT_TYPE.CODE_VALUE = E.ADMIT_TYPE_CD)
LEFT JOIN CLINICAL_EVENT ESDP ON (ESDP.ENCNTR_ID = E.ENCNTR_ID)
AND ESDP.EVENT_CD = :p18
AND ESDP.RESULT_STATUS_CD IN (:p19, :p20, :p21)
AND ESDP.RECORD_STATUS_CD = 188 -- ACTIVE
AND ESDP.VALID_UNTIL_DT_TM > cclsql_cnvtdatetimeutc(SYSDATE, 1, 126)
LEFT JOIN (
SELECT O.* FROM ORDERS O WHERE O.ORDER_ID = (
SELECT MAX(O2.ORDER_ID) FROM ORDERS O2 WHERE O2.ENCNTR_ID = E.ENCNTR_ID AND O2.CATALOG_CD = :p22 AND O2.ACTIVE_IND = 1
)) O ON
O.ENCNTR_ID = E.ENCNTR_ID
-- LEFT OUTER JOIN (
-- SELECT * FROM ORDER_ACTION WHERE ORDER_ID = O.ORDER_ID AND ACTION_TYPE_CD IN (:p23, :p24) AND ACTION_SEQUENCE = (
-- SELECT MAX(OA2.ACTION_SEQUENCE)
-- FROM ORDER_ACTION OA2
-- WHERE OA2.ORDER_ID = O.ORDER_ID AND OA2.ACTION_TYPE_CD IN (:p23, :p24)
-- )) OA ON
-- O.ORDER_ID = OA.ORDER_ID
WHERE E.ENCNTR_ID IN (:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7, :p8, :p9, :p10, :p11, :p12, :p13, :p14, :p15, :p16)
ORDER BY PERSON_ID ASC, REG_DT_TM DESC;
E.ENCNTR_ID on the third line below will not be found, because this subquery is outside the scope of the parent query, so you'll need to join your E table in this subquery. However, since you need the ENCTR_ID specificity in this subquery, you should consider using a CTE (WITH Clause) to prefetch this table.
LEFT JOIN (
SELECT O.* FROM ORDERS O WHERE O.ORDER_ID = (
SELECT MAX(O2.ORDER_ID) FROM ORDERS O2 WHERE O2.ENCNTR_ID = E.ENCNTR_ID AND
O2.CATALOG_CD = :p22 AND O2.ACTIVE_IND = 1
)) O ON
O.ENCNTR_ID = E.ENCNTR_ID
It seems the main thing I was lacking was using a GROUP BY in my subquery. I didn't need to use a derived table at all. Just use a subquery with GROUP BY and duplicate the qualifications in the main join query and the subquery.
SELECT
E.ENCNTR_ID
,E.REASON_FOR_VISIT
-- ,D.DIAGNOSIS_DISPLAY AS DISCHARGE_DX
-- ,D.DIAG_FTDESC AS WORKING_DX
,cclsql_cnvtdatetimeutc(E.REG_DT_TM, 2, 126) as reg_dt_tm
,cclsql_cnvtdatetimeutc(E.DISCH_DT_TM, 2, 126) as disch_dt_tm
,FACILITY.DISPLAY AS FACILITY
,P.PERSON_ID
,P.NAME_FULL_FORMATTED
,cclsql_cnvtdatetimeutc(P.BIRTH_DT_TM, 2, P.BIRTH_TZ) as birth_dt_tm
,MRN.ALIAS AS MRN
,FIN.ALIAS AS FIN
,ENC_TYPE.DISPLAY AS ENCOUNTER_TYPE
,NURSE_UNIT.DISPLAY AS NURSE_UNIT
,ROOM.DISPLAY AS ROOM
,BED.DISPLAY AS BED
-- ,ADMITDOC_NAME.NAME_FULL_FORMATTED AS ADMITDOC_NAME
,DCDOC_NAME.NAME_FULL_FORMATTED AS DCDOC_NAME
,ADMIT_SRC.DISPLAY AS ADMIT_SOURCE
,ADMIT_TYPE.DISPLAY AS ADMIT_TYPE
,ESDP.RESULT_VAL AS ESDP
,DISCH_DISPOSITION.DISPLAY AS DISCH_DISPOSITION
FROM ENCOUNTER E
-- JOIN DISCHARGE D ON (D.ENCNTR_ID = E.ENCNTR_ID)
-- AND D.ACTIVE_IND = 1
-- AND D.END_EFFECTIVE_DT_TM > SYSDATE
JOIN PERSON P ON (P.PERSON_ID = E.PERSON_ID)
JOIN CODE_VALUE ENC_TYPE ON (ENC_TYPE.CODE_VALUE = E.ENCNTR_TYPE_CLASS_CD)
JOIN ENCNTR_ALIAS FIN ON (FIN.ENCNTR_ID = E.ENCNTR_ID)
AND FIN.ENCNTR_ALIAS_TYPE_CD = 1077 -- FIN CODE
AND FIN.ACTIVE_IND = 1
AND FIN.ACTIVE_STATUS_CD = 188
JOIN ENCNTR_ALIAS MRN ON (MRN.ENCNTR_ID = E.ENCNTR_ID)
AND MRN.ENCNTR_ALIAS_TYPE_CD = 1079 -- MRN CODE
AND MRN.ACTIVE_IND = 1
AND MRN.ACTIVE_STATUS_CD = 188
JOIN CODE_VALUE FACILITY on (FACILITY.CODE_VALUE = E.LOC_FACILITY_CD)
JOIN CODE_VALUE NURSE_UNIT on (NURSE_UNIT.CODE_VALUE = E.LOC_NURSE_UNIT_CD)
JOIN CODE_VALUE ROOM on (ROOM.CODE_VALUE = E.LOC_ROOM_CD)
JOIN CODE_VALUE BED on (BED.CODE_VALUE = E.LOC_BED_CD)
JOIN CODE_VALUE DISCH_DISPOSITION on (DISCH_DISPOSITION.CODE_VALUE = E.DISCH_DISPOSITION_CD)
LEFT JOIN ORDERS O ON
O.ENCNTR_ID = E.ENCNTR_ID AND O.CATALOG_CD = :p22 AND O.ACTIVE_IND = 1 AND O.ORDER_ID IN (
SELECT MAX(O2.ORDER_ID) FROM ORDERS O2 WHERE O2.ENCNTR_ID = E.ENCNTR_ID AND O2.CATALOG_CD = :p22 AND O2.ACTIVE_IND = 1 GROUP BY O2.ENCNTR_ID
)
LEFT JOIN ORDER_ACTION OA ON
OA.ORDER_ID = O.ORDER_ID AND OA.ACTION_TYPE_CD IN (:p23, :p24) AND OA.ACTION_SEQUENCE IN (
SELECT MAX(OA2.ACTION_SEQUENCE) FROM ORDER_ACTION OA2 WHERE OA2.ORDER_ID = O.ORDER_ID AND OA2.ACTION_TYPE_CD IN (:p23, :p24) GROUP BY OA2.ORDER_ID
)
LEFT JOIN PRSNL ADMITDOC_NAME ON (ADMITDOC_NAME.PERSON_ID = OA.ORDER_PROVIDER_ID)
LEFT JOIN CLINICAL_EVENT HOSP_SUM ON (HOSP_SUM.ENCNTR_ID = E.ENCNTR_ID)
AND HOSP_SUM.EVENT_CD = :p17
AND HOSP_SUM.RESULT_STATUS_CD IN (:p19, :p20, :p21)
AND HOSP_SUM.RECORD_STATUS_CD = 188 -- ACTIVE
AND HOSP_SUM.VALID_UNTIL_DT_TM > cclsql_cnvtdatetimeutc(SYSDATE, 1, 126)
LEFT JOIN PRSNL DCDOC_NAME ON (DCDOC_NAME.PERSON_ID = HOSP_SUM.VERIFIED_PRSNL_ID)
JOIN CODE_VALUE ADMIT_SRC ON (ADMIT_SRC.CODE_VALUE = E.ADMIT_SRC_CD)
JOIN CODE_VALUE ADMIT_TYPE ON (ADMIT_TYPE.CODE_VALUE = E.ADMIT_TYPE_CD)
LEFT JOIN CLINICAL_EVENT ESDP ON (ESDP.ENCNTR_ID = E.ENCNTR_ID)
AND ESDP.EVENT_CD = :p18
AND ESDP.RESULT_STATUS_CD IN (:p19, :p20, :p21)
AND ESDP.RECORD_STATUS_CD = 188 -- ACTIVE
AND ESDP.VALID_UNTIL_DT_TM > cclsql_cnvtdatetimeutc(SYSDATE, 1, 126)
WHERE E.ENCNTR_ID IN (:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7, :p8, :p9, :p10, :p11, :p12, :p13, :p14, :p15, :p16)
ORDER BY PERSON_ID ASC, REG_DT_TM DESC

Most performant way to query products from multiple tables and combine results?

I essentially have two core categories of products in a system with multiple manufacturers. The current query in the system was written to handle only one product.
Here is the code:
SELECT DISTINCT c.id
FROM "Config" c
JOIN "FirstProduct" fp ON c.id = fp.widget
JOIN "TheSupplier" ts ON ts.name = fp.manufacturer
LEFT JOIN "ProductOffer" o ON (o.app = ts.app)
WHERE ts.app = ?appId
AND (?mlid IS NULL OR ?mlid = fp.manufacturer)
AND (o.company IS NULL OR o.manufacturer <> fp.manufacturer)
What I want to get to: adding a query for another table, which I think can be done in two ways:
Effectively do both queries in one query
SELECT DISTINCT c.id
FROM "Config" c
JOIN "FirstProduct" fp ON c.id = fp.widget fpw
JOIN "SecondProduct" sp ON c.id = sp.widget spw
JOIN "TheSupplier" ts ON ts.name = fp.manufacturer
LEFT JOIN "ProductOffer" o ON (o.app = ts.app)
WHERE ts.app = ?appId
AND (?mlid IS NULL OR ?mlid = fp.manufacturer)
AND (o.company IS NULL OR o.manufacturer <> fp.manufacturer)
Do two separate queries, and UNION them.
with first_product_table as (
SELECT DISTINCT c.id
FROM "Config" c
JOIN "FirstProduct" fp ON c.id = fp.widget
JOIN "TheSupplier" ts ON ts.name = fp.manufacturer
LEFT JOIN "ProductOffer" o ON (o.app = ts.app)
WHERE ts.app = ?appId
AND (?mlid IS NULL OR ?mlid = fp.manufacturer)
AND (o.company IS NULL OR o.manufacturer <> fp.manufacturer)
) ,
second_product_table as (
SELECT DISTINCT c.id
FROM "Config" c
JOIN "SecondProduct" sp ON c.id = sp.widget spw
JOIN "TheSupplier" ts ON ts.name = fp.manufacturer
LEFT JOIN "ProductOffer" o ON (o.app = ts.app)
WHERE ts.app = ?appId
AND (?mlid IS NULL OR ?mlid = fp.manufacturer)
AND (o.company IS NULL OR o.manufacturer <> fp.manufacturer)
)
select * from first_product_table
UNION
second_product_table
This will be in a production system, so performance concerns are non-trivial.
Those two queries are not equivalent. According to your description the second one is the one that will produce the results you want. I made a small fix at the end:
with
first_product_table as (
SELECT DISTINCT c.id
FROM "Config" c
JOIN "FirstProduct" fp ON c.id = fp.widget
JOIN "TheSupplier" ts ON ts.name = fp.manufacturer
LEFT JOIN "ProductOffer" o ON o.app = ts.app
WHERE ts.app = ?appId
AND (?mlid IS NULL OR ?mlid = fp.manufacturer)
AND (o.company IS NULL OR o.manufacturer <> fp.manufacturer)
),
second_product_table as (
SELECT DISTINCT c.id
FROM "Config" c
JOIN "SecondProduct" sp ON c.id = sp.widget spw
JOIN "TheSupplier" ts ON ts.name = fp.manufacturer
LEFT JOIN "ProductOffer" o ON (o.app = ts.app)
WHERE ts.app = ?appId
AND (?mlid IS NULL OR ?mlid = fp.manufacturer)
AND (o.company IS NULL OR o.manufacturer <> fp.manufacturer)
)
select * from first_product_table
UNION
select * from second_product_table
If you are concerned about performance you'll need to make sure you have the right indexes. The follwing two indexes are the most important ones when the parameter mlid is specified:
create index ix1 on FirstProduct (manufacturer);
create index ix2 on SecondProduct (manufacturer);
Then, if the query is still slow, you can try:
create index ix3 on TheSupplier (name, app);
create index ix4 on ProductOffer (app, company, manufacturer);
Final Note: Please consider the condition o.manufacturer <> fp.manufacturer may filter out rows you may want, since it efectively negates the outer join, converting it into an inner join. Maybe you want the last four lines of each section look like:
LEFT JOIN "ProductOffer" o ON (o.app = ts.app)
AND (o.company IS NULL OR o.manufacturer <> fp.manufacturer)
WHERE ts.app = ?appId
AND (?mlid IS NULL OR ?mlid = fp.manufacturer)
But this is just a guess since I don't fully understand your requirements.

SQL DB2 how to apply date variable to inner queries?

I have date variable that I have applied to the outer query, but I am receiving an error message: v_Date is not valid in the context where it is used - when trying to use it on the inner queries.
Can you help me replace the two 3/31/2016 dates in the inner query with the time variable v_Date? Do I need to move the WITH line or do a double join?
WITH ttt as (select '3/31/2016' v_Date FROM SYSIBM.SYSDUMMY1)
SELECT
fpr.ID
fpr.ID_PRICG_MTHDY,
fpr.MARKET_DATE,
fpr.PRICE_AMOUNT AS FIRST_PRICE,
pr.PRICE_AMOUNT AS SECOND_PRICE,
pr2.PRICE_AMOUNT AS THIRD_PRICE,
thr.PRICE_DIFF AS TEST_1,
thr.PRICE_DIFF2 AS TEST_2
FROM PRICE_TABLE_1 fpr
LEFT JOIN PRICE_TABLE0 pr
ON fpr.ID = pr.ID AND pr.MARKET_DATE = '3/31/2016' AND pr.ROLE_TYPE = 'D'
LEFT JOIN PRICE_TABLE0 pr2
ON fpr.ID = pr2.ID and pr2.MARKET_DATE = '3/31/2016' AND pr2.ROLE_TYPE = 'P'
LEFT JOIN THRESHOLD_TEST_TABLE thr
ON fpr.ID_PRICG_MTHDY = thr.ID_PRICG_MTHDY
JOIN ttt ON fpr.MARKET_DATE = v_Date
WHERE fpr.DT_EXPTN = '1/1/9999'
AND fpr.MARKET_DATE = fpr.SELL_DATE
AND fpr.TYPE = 'F'
AND fpr.ID_PRICG_MTHDY IN (1, 3, 4, 7)
I would start with the CTE as the first element of the FROM and then you can use the value anywhere:
FROM ttt LEFT JOIN
PRICE_TABLE_1 fpr
ON fpr.MARKET_DATE = v_Date LEFT JOIN
PRICE_TABLE0 pr
ON fpr.ID = pr.ID AND pr.MARKET_DATE = ttt.v_date AND pr.ROLE_TYPE = 'D' LEFT JOIN
PRICE_TABLE0 pr2
ON fpr.ID = pr2.ID and pr2.MARKET_DATE = ttt.v_date AND pr2.ROLE_TYPE = 'P' LEFT JOIN
THRESHOLD_TEST_TABLE thr
ON fpr.ID_PRICG_MTHDY = thr.ID_PRICG_MTHDY

sql 2008 error 'the column prod_desc1 was specified multiple times for "a"'

I've got this ginormous sql select statement I'm using in a SSRS 2008 report. I need field "prod_desc1" from the Product table, but also the same named field from Order_Line. I've aliased both of them but I'm still getting the error. Here's my statment:
SELECT order_num, quote_num, cust_num, cust_desc, ship_via_desc, whse_desc, slsm_desc, ar_term_desc, vend_desc, pline_desc, major_grp, cust_po_num, ord_date, req_date, shp_date, ord_type, tot_ord_$, pline_num, prod_num, price_ext, STATUS, prod_desc1 as prod_desc, prod_desc1 as com1, prod_desc2 as com2, net_price, seq_num
FROM (SELECT o.order_num, o.quote_num, o.cust_num, c.cust_desc, s.ship_via_desc, w.whse_desc, sa.slsm_desc, t.ar_term_desc, v.vend_desc, ca.pline_desc, ca.major_grp, o.cust_po_num, o.ord_date, o.req_date, o.shp_date, o.ord_type, o.tot_ord_$, ol. pline_num, ol.prod_num, price_ext, 'Open' AS STATUS, p.prod_desc1, ol.prod_desc1, ol.prod_desc2, ol.net_price, seq_num
FROM [ORDER] o
left outer join order_line ol on o.order_num = ol.order_num
left outer join product p on ol.prod_num = p.prod_num
left outer join customer c on o.cust_num = c.cust_no
left outer join ship_via s on o.ship_via_num = s.ship_via_id
left outer join whse_addr w on o.shp_whse = w.whse_num
left outer join salesman sa on o.slsm2_num = sa.slsm_num
left outer join terms t on o.ar_term_num = t.ar_term_num
left outer join vend v on ol.prim_vend_num = v.vend_num
left outer join category ca on ol.pline_Num = ca.pline_Id
WHERE (#OrderNum is null OR o.order_num = (#OrderNum)) and
o.cust_num IN (#CustomerNumber) and
(ol.prim_vend_num IN (#VendNum) or ol.prim_vend_num is NULL) and
o.req_date between (#ReqDateFrom) and (#ReqDateTo) and
o.ord_date between (#EntDateFrom) and (#EntDateTo) and
(c.slsm_num = (#SlsmnRealNum) and ol.major_grp IN ('C', 'R', 'W') or c.slsm2_num = (#SlsmnRealNum) and ol.major_grp IN ('P', 'S')) and
ol.major_grp IN (#MajorGrp) and
ol.pline_num IN (#ProductLine) and
(#PONum is null OR o.cust_po_num LIKE ('%' + #PONum + '%')) and
(#ItemNum is null OR ol.prod_num = (#ItemNum))
UNION ALL
SELECT o.order_num, o.quote_num, o.cust_num, c.cust_desc, s.ship_via_desc, w.whse_desc, sa.slsm_desc, t.ar_term_desc, v.vend_desc, ca.pline_desc, ca.major_grp, o.cust_po_num, o.ord_date, o.req_date, o.shp_date, o.ord_type, o.tot_ord_$, ol.pline_num, ol.prod_num, net_ext, 'Closed' AS STATUS, p.prod_desc1, ol.prod_desc1, ol.prod_desc2, ol.net_price, seq_num
FROM ORDER_HISTORY o
left outer join order_history_line ol on o.order_num = ol.order_num
left outer join product p on ol.prod_num = p.prod_num
left outer join customer c on o.cust_num = c.cust_no
left outer join ship_via s on o.ship_via_num = s.ship_via_id
left outer join whse_addr w on o.shp_whse = w.whse_num
left outer join salesman sa on o.slsm2_num = sa.slsm_num
left outer join terms t on o.ar_term_num = t.ar_term_num
left outer join vend v on ol.prim_vend_num = v.vend_num
left outer join category ca on ol.pline_Num = ca.pline_Id
WHERE (#OrderNum is null OR o.order_num = (#OrderNum)) and
o.cust_num IN (#CustomerNumber) and
(ol.prim_vend_num IN (#VendNum) or ol.prim_vend_num is NULL) and
o.req_date between (#ReqDateFrom) and (#ReqDateTo) and
o.ord_date between (#EntDateFrom) and (#EntDateTo) and
(c.slsm_num = (#SlsmnRealNum) and ol.major_grp IN ('C', 'R', 'W') or c.slsm2_num = (#SlsmnRealNum) and ol.major_grp IN ('P', 'S')) and
ol.major_grp IN (#MajorGrp) and
ol.pline_num IN (#ProductLine) and
(#PONum is null OR o.cust_po_num LIKE ('%' + #PONum + '%')) and
(#ItemNum is null OR ol.prod_num = (#ItemNum))) as a
WHERE (STATUS = CASE WHEN #OrderType = 'Both' THEN STATUS ELSE #OrderType END) ORDER BY cust_desc, req_date, vend_desc
any help would be greatly appreciated! :)

Add a filter parameter to ssrs report

I have a query that I need to update to allow user to filter out pending applications. I have created the parameter and tried to implement using case but it is not working or giving any error messages on how to correct it. The code is:
select distinct pers.person_fname,
pers.person_mname,
pers.person_lname,
le.nationalprovidernumber NPN,
lic.licensenumber LICENSE_NUMBER,
adr.address_line1 ADDRESS1,
adr.address_line2 ADDRESS2,
adr.address_line3 ADDRESS3,
adr.city CITY,
sp.state_province_name STATE,
adr.postal_code ZIP_CODE,
eml.email,
rtp.residencetype_name RESIDENCY,
ltp.licensetype_name LICENSE_TYPE,
lic.expirationdate DATE_OF_EXPIRATION
from odilic_admin.license lic
inner join odilic_admin.licenseststimeline lst
on lic.license_id = lst.license_id
inner join odilic_admin.licenseststype lstp
on lst.licenseststype_id = lstp.licenseststype_id
inner join odilic_admin.licensedef ldef
on lic.licensedef_id = ldef.licensedef_id
inner join odilic_admin.licensetype ltp
on ldef.licensetype_id = ltp.licensetype_id
inner join odilic_admin.residencetype rtp
on ldef.residencetype_id = rtp.residencetype_id
inner join odilic_admin.licensingentity le
on lic.licensingentity_id = le.licensingentity_id
inner join odilic_admin.individual ind
on le.licensingentity_id = ind.licensingentity_id
inner join odidir_admin.person pers
on ind.person_id = pers.person_id
left outer join odidir_admin.person_address_rel par
on pers.person_id = par.person_id
left outer join odidir_admin.address adr
on par.address_id = adr.address_id
left outer join odidir_admin.address_type atp
on adr.address_type_id = atp.address_type_id
left outer join odidir_admin.state_province sp
on adr.state_province_id = sp.state_province_id
left outer join
(select pr.person_id, em.email_id, em.email
from odidir_admin.person pr,
odidir_admin.person_email_rel pe,
odidir_admin.email em
where pr.person_id = pe.person_id
and pe.email_id = em.email_id
and email_type_id = 2) eml
on pers.person_id = eml.person_id
where
ltp.licensetype_id in (:License_type)
and lstp.licenseststype_name = 'Active'
and atp.address_type_name = 'Mailing Licensing'
and (lic.expirationdate >= current_date and
trunc(lic.expirationdate) = :Expiration_Date)
and sysdate between lst.periodbegindate and lst.periodenddate
order by lic.licensenumber
In order to get applications that are pending I need to access the table odilic_admin.licenseappl and filter out all licenses with appststype = 2 (pending). To do this I added a join to the query before the last left outer join andt hen a case at bottom for when this parameter is selected.
select distinct pers.person_fname,
pers.person_mname,
pers.person_lname,
le.nationalprovidernumber NPN,
lic.licensenumber LICENSE_NUMBER,
adr.address_line1 ADDRESS1,
adr.address_line2 ADDRESS2,
adr.address_line3 ADDRESS3,
adr.city CITY,
sp.state_province_name STATE,
adr.postal_code ZIP_CODE,
eml.email,
rtp.residencetype_name RESIDENCY,
ltp.licensetype_name LICENSE_TYPE,
lic.expirationdate DATE_OF_EXPIRATION
from odilic_admin.license lic
inner join odilic_admin.licenseststimeline lst
on lic.license_id = lst.license_id
inner join odilic_admin.licenseststype lstp
on lst.licenseststype_id = lstp.licenseststype_id
inner join odilic_admin.licensedef ldef
on lic.licensedef_id = ldef.licensedef_id
inner join odilic_admin.licensetype ltp
on ldef.licensetype_id = ltp.licensetype_id
inner join odilic_admin.residencetype rtp
on ldef.residencetype_id = rtp.residencetype_id
inner join odilic_admin.licensingentity le
on lic.licensingentity_id = le.licensingentity_id
inner join odilic_admin.individual ind
on le.licensingentity_id = ind.licensingentity_id
inner join odidir_admin.person pers
on ind.person_id = pers.person_id
left outer join odidir_admin.person_address_rel par
on pers.person_id = par.person_id
left outer join odidir_admin.address adr
on par.address_id = adr.address_id
left outer join odidir_admin.address_type atp
on adr.address_type_id = atp.address_type_id
left outer join odidir_admin.state_province sp
on adr.state_province_id = sp.state_province_id
**left outer join odilic_admin.licenseappl appl
on lic.licensingentity_id = appl.licenseappl_id**
left outer join
(select pr.person_id, em.email_id, em.email
from odidir_admin.person pr,
odidir_admin.person_email_rel pe,
odidir_admin.email em
where pr.person_id = pe.person_id
and pe.email_id = em.email_id
and email_type_id = 2) eml
on pers.person_id = eml.person_id
where
ltp.licensetype_id in (:License_type)
and lstp.licenseststype_name = 'Active'
and atp.address_type_name = 'Mailing Licensing'
and (lic.expirationdate >= current_date and
trunc(lic.expirationdate) = :Expiration_Date)
and sysdate between lst.periodbegindate and lst.periodenddate
**case :pending when = yes then appl.applststype_id !=2
end**
order by lic.licensenumber
Instead of the case I have also tried using an IF with the same result. This looks like:
if :Pending = 1
then
and appl.applststype_id != 2;
end if;
Any help to get me past this is greatly appreciated and I will be sure to vote and select most correct answer to help me solve this.
Assuming that your :pending parameter is a numeric where a value of 1 indicates that pending licences are to be excluded and you only want to exclude licence applications that are pending, try adding the following condition in place of your existing case clause:
and (:pending <> 1 or appl.applststype_id !=2)