HANA SQL Performance - sql

Can someone help me to reshape this code in a proper way. This Code is old one and would like to reshape it. But not sure how to do it better.
SELECT COUNT(*) FROM (
SELECT SRC.CLIENT,SRC.GUID
,COALESCE(SRC.PARTNER_ZSCHEDUL,'') PARTNER_ZSCHEDUL
,COALESCE(SRC.PARTNER_ZSVCUNIT,'') PARTNER_ZSVCUNIT
,COALESCE(SRC.PARTNER_ZPARTAPP,'') PARTNER_ZPARTAPP
,COALESCE(SRC.PARTNER_ZESCOWN,'') PARTNER_ZESCOWN
,COALESCE(SRC.PARTNER_ZESCAPRV,'') PARTNER_ZESCAPRV
,COALESCE(SRC.PARTNER_ZESCRQST,'') PARTNER_ZESCRQST
,COALESCE(SRC.PARTNER_ZPARTORD,'') PARTNER_ZPARTORD
,COALESCE(SRC.PARTNER_ER,'') PARTNER_ER
FROM KCP810_HBP.ZCRMD_ORDERADM_I_EXTN TGT, (
SELECT CLIENT,GUID
, MIN(CASE WHEN PARTNER_FCT = 'ZSCHEDUL' THEN PARTNER END) PARTNER_ZSCHEDUL
, MIN(CASE WHEN PARTNER_FCT = 'ZSVCUNIT' THEN MERGED_PARTNER END) PARTNER_ZSVCUNIT
, MIN(CASE WHEN PARTNER_FCT = 'ZSVCUNIT' THEN PARTNER END) PARTNER_ZSVCUNIT_HR
, MIN(CASE WHEN PARTNER_FCT = 'ZPARTAPP' THEN PARTNER END) PARTNER_ZPARTAPP
, MIN(CASE WHEN PARTNER_FCT = 'ZESCOWN' THEN PARTNER END) PARTNER_ZESCOWN
, MIN(CASE WHEN PARTNER_FCT = 'ZESCAPRV' THEN PARTNER END) PARTNER_ZESCAPRV
, MIN(CASE WHEN PARTNER_FCT = 'ZESCRQST' THEN PARTNER END) PARTNER_ZESCRQST
, MIN(CASE WHEN PARTNER_FCT = 'ZPARTORD' THEN PARTNER END) PARTNER_ZPARTORD
, MIN(CASE WHEN PARTNER_FCT = '00000014' THEN PARTNER END) PARTNER_ER
FROM "_SYS_BIC"."CRM-SV.A-COMMON-VIEWS.Proc.Item/CV_IT_01_PARTNER_ALL" GROUP BY CLIENT,GUID ) SRC
WHERE TGT.CLIENT = SRC.CLIENT
AND TGT.GUID = SRC.GUID
AND ( TGT.PARTNER_ZSCHEDUL != SRC.PARTNER_ZSCHEDUL OR TGT.PARTNER_ZSVCUNIT != SRC.PARTNER_ZSVCUNIT OR
TGT.PARTNER_ZPARTAPP != SRC.PARTNER_ZPARTAPP OR TGT.PARTNER_ZESCOWN != SRC.PARTNER_ZESCOWN OR
TGT.PARTNER_ZESCAPRV != SRC.PARTNER_ZESCAPRV OR TGT.PARTNER_ZESCRQST != SRC.PARTNER_ZESCRQST OR
TGT.PARTNER_ZPARTORD != SRC.PARTNER_ZPARTORD OR TGT.PARTNER_00000014 != SRC.PARTNER_ER));

To improve the speed, if you want to keep using SQL script, firstly you should replace the where statements with a join statement like below:
SELECT COUNT(*) FROM (
SELECT SRC.CLIENT,SRC.GUID
,COALESCE(SRC.PARTNER_ZSCHEDUL,'') PARTNER_ZSCHEDUL
,COALESCE(SRC.PARTNER_ZSVCUNIT,'') PARTNER_ZSVCUNIT
,COALESCE(SRC.PARTNER_ZPARTAPP,'') PARTNER_ZPARTAPP
,COALESCE(SRC.PARTNER_ZESCOWN,'') PARTNER_ZESCOWN
,COALESCE(SRC.PARTNER_ZESCAPRV,'') PARTNER_ZESCAPRV
,COALESCE(SRC.PARTNER_ZESCRQST,'') PARTNER_ZESCRQST
,COALESCE(SRC.PARTNER_ZPARTORD,'') PARTNER_ZPARTORD
,COALESCE(SRC.PARTNER_ER,'') PARTNER_ER
FROM KCP810_HBP.ZCRMD_ORDERADM_I_EXTN TGT
INNER JOIN (
SELECT CLIENT,GUID
, MIN(CASE WHEN PARTNER_FCT = 'ZSCHEDUL' THEN PARTNER END) PARTNER_ZSCHEDUL
, MIN(CASE WHEN PARTNER_FCT = 'ZSVCUNIT' THEN MERGED_PARTNER END) PARTNER_ZSVCUNIT
, MIN(CASE WHEN PARTNER_FCT = 'ZSVCUNIT' THEN PARTNER END) PARTNER_ZSVCUNIT_HR
, MIN(CASE WHEN PARTNER_FCT = 'ZPARTAPP' THEN PARTNER END) PARTNER_ZPARTAPP
, MIN(CASE WHEN PARTNER_FCT = 'ZESCOWN' THEN PARTNER END) PARTNER_ZESCOWN
, MIN(CASE WHEN PARTNER_FCT = 'ZESCAPRV' THEN PARTNER END) PARTNER_ZESCAPRV
, MIN(CASE WHEN PARTNER_FCT = 'ZESCRQST' THEN PARTNER END) PARTNER_ZESCRQST
, MIN(CASE WHEN PARTNER_FCT = 'ZPARTORD' THEN PARTNER END) PARTNER_ZPARTORD
, MIN(CASE WHEN PARTNER_FCT = '00000014' THEN PARTNER END) PARTNER_ER
FROM "_SYS_BIC"."CRM-SV.A-COMMON-VIEWS.Proc.Item/CV_IT_01_PARTNER_ALL" GROUP BY CLIENT,GUID ) SRC
ON TGT.CLIENT = SRC.CLIENT
AND TGT.GUID = SRC.GUID
AND ( TGT.PARTNER_ZSCHEDUL != SRC.PARTNER_ZSCHEDUL
OR TGT.PARTNER_ZSVCUNIT != SRC.PARTNER_ZSVCUNIT
OR TGT.PARTNER_ZPARTAPP != SRC.PARTNER_ZPARTAPP
OR TGT.PARTNER_ZESCOWN != SRC.PARTNER_ZESCOWN
OR TGT.PARTNER_ZESCAPRV != SRC.PARTNER_ZESCAPRV
OR TGT.PARTNER_ZESCRQST != SRC.PARTNER_ZESCRQST
OR TGT.PARTNER_ZPARTORD != SRC.PARTNER_ZPARTORD
OR TGT.PARTNER_00000014 != SRC.PARTNER_ER));
If this doesn't help you, you should use HANA's Analytic Views for which you could read here.

Related

Converting SQL Query in to Linq or in lambda

I am trying to convert it in LINQ but I am not getting success , I would appreciate if someone help me out to convert it in linq or write the query using lambda expression
SELECT MAX('Quality Of Service') AS Category,
COUNT(CASE WHEN t.QualityOfService = 'Excellent' THEN 1 END) AS Excellent,
COUNT(CASE WHEN t.QualityOfService = 'VeryGood' THEN 1 END) AS Very_Good,
COUNT(CASE WHEN t.QualityOfService = 'Good' THEN 1 END) AS Good,
COUNT(CASE WHEN t.QualityOfService = 'Bad' THEN 1 END) AS Bad,
COUNT(CASE WHEN t.QualityOfService = 'Poor' THEN 1 END) AS Poor
FROM Feedbacks t
WHERE t.DateOfVisit BETWEEN '2018-03-29' AND '2018-03-29'
UNION
SELECT MAX('Quality Of Food') AS Category,
COUNT(CASE WHEN t.QualityOfFood = 'Excellent' THEN 1 END) AS Excellent,
COUNT(CASE WHEN t.QualityOfFood = 'VeryGood' THEN 1 END) AS Very_Good,
COUNT(CASE WHEN t.QualityOfFood = 'Good' THEN 1 END) AS Good,
COUNT(CASE WHEN t.QualityOfFood = 'Bad' THEN 1 END) AS Bad,
COUNT(CASE WHEN t.QualityOfFood = 'Poor' THEN 1 END) AS Poor
FROM Feedbacks t
WHERE t.DateOfVisit BETWEEN '2018-03-29' AND '2018-03-29'
UNION
SELECT MAX('Cleanliness Of Lounge') AS Category,
COUNT(CASE WHEN t.CleanlinessOfLounge = 'Excellent' THEN 1 END) AS Excellent,
COUNT(CASE WHEN t.CleanlinessOfLounge = 'VeryGood' THEN 1 END) AS Very_Good,
COUNT(CASE WHEN t.CleanlinessOfLounge = 'Good' THEN 1 END) AS Good,
COUNT(CASE WHEN t.CleanlinessOfLounge = 'Bad' THEN 1 END) AS Bad,
COUNT(CASE WHEN t.CleanlinessOfLounge = 'Poor' THEN 1 END) AS Poor
FROM Feedbacks t
WHERE t.DateOfVisit BETWEEN '2018-03-29' AND '2018-03-29'
UNION
SELECT MAX('Friendliness Of Staff') AS Category,
COUNT(CASE WHEN t.FriendlinessOfStaff = 'Excellent' THEN 1 END) AS Excellent,
COUNT(CASE WHEN t.FriendlinessOfStaff = 'VeryGood' THEN 1 END) AS Very_Good,
COUNT(CASE WHEN t.FriendlinessOfStaff = 'Good' THEN 1 END) AS Good,
COUNT(CASE WHEN t.FriendlinessOfStaff = 'Bad' THEN 1 END) AS Bad,
COUNT(CASE WHEN t.FriendlinessOfStaff = 'Poor' THEN 1 END) AS Poor
FROM Feedbacks t
WHERE t.DateOfVisit BETWEEN '2018-03-29' AND '2018-03-29'
UNION
SELECT MAX('Overall Experience') AS Category,
COUNT(CASE WHEN t.OverAllExperience = 'Excellent' THEN 1 END) AS Excellent,
COUNT(CASE WHEN t.OverAllExperience = 'VeryGood' THEN 1 END) AS Very_Good,
COUNT(CASE WHEN t.OverAllExperience = 'Good' THEN 1 END) AS Good,
COUNT(CASE WHEN t.OverAllExperience = 'Bad' THEN 1 END) AS Bad,
COUNT(CASE WHEN t.OverAllExperience = 'Poor' THEN 1 END) AS Poor
FROM Feedbacks t
WHERE t.DateOfVisit BETWEEN '2018-03-29' AND '2018-03-29'
The result of this query is coming in this way
Category Excellent Very_Good Good Bad Poor
Null 0 0 0 0 0
I have zero feedbacks right now in my table that is why it is showing null in category and all zero's.
My table looks like this
ID QualityOfFood QualityOfServices CleanlinessOfLounge FreindlinessOfStaff OverALLExperience
I asked this type of question but "this type" not the same , so requesting to don't mark it dublicate
I've Found it by using linqer
( from t in
( from t in db.Feedbacks
where
t.DateOfVisit >= "2018-03-29" && t.DateOfVisit <= "2018-03-29"
select new {
Column1 = "Quality Of Service",
Column2 =
t.QualityOfService == "Excellent" ? (System.Int64?)1 : null,
Column3 =
t.QualityOfService == "VeryGood" ? (System.Int64?)1 : null,
Column4 =
t.QualityOfService == "Good" ? (System.Int64?)1 : null,
Column5 =
t.QualityOfService == "Bad" ? (System.Int64?)1 : null,
Column6 =
t.QualityOfService == "Poor" ? (System.Int64?)1 : null,
Dummy = "x"
})
group t by new { t.Dummy } into g
select new {
Category = g.Max(p => "Quality Of Service"),
Excellent = g.Count(p => p.Column2 != null),
Very_Good = g.Count(p => p.Column3 != null),
Good = g.Count(p => p.Column4 != null),
Bad = g.Count(p => p.Column5 != null),
Poor = g.Count(p => p.Column6 != null)
}
).Union
( from t in
( from t in db.Feedbacks
where
t.DateOfVisit >= "2018-03-29" && t.DateOfVisit <= "2018-03-29"
select new {
Column1 = "Quality Of Food",
Column2 =
t.QualityOfFood == "Excellent" ? (System.Int64?)1 : null,
Column3 =
t.QualityOfFood == "VeryGood" ? (System.Int64?)1 : null,
Column4 =
t.QualityOfFood == "Good" ? (System.Int64?)1 : null,
Column5 =
t.QualityOfFood == "Bad" ? (System.Int64?)1 : null,
Column6 =
t.QualityOfFood == "Poor" ? (System.Int64?)1 : null,
Dummy = "x"
})
group t by new { t.Dummy } into g
select new {
Category = g.Max(p => "Quality Of Food"),
Excellent = g.Count(p => p.Column2 != null),
Very_Good = g.Count(p => p.Column3 != null),
Good = g.Count(p => p.Column4 != null),
Bad = g.Count(p => p.Column5 != null),
Poor = g.Count(p => p.Column6 != null)
}
).Union
( from t in
( from t in db.Feedbacks
where
t.DateOfVisit >= "2018-03-29" && t.DateOfVisit <= "2018-03-29"
select new {
Column1 = "Cleanliness Of Lounge",
Column2 =
t.CleanlinessOfLounge == "Excellent" ? (System.Int64?)1 : null,
Column3 =
t.CleanlinessOfLounge == "VeryGood" ? (System.Int64?)1 : null,
Column4 =
t.CleanlinessOfLounge == "Good" ? (System.Int64?)1 : null,
Column5 =
t.CleanlinessOfLounge == "Bad" ? (System.Int64?)1 : null,
Column6 =
t.CleanlinessOfLounge == "Poor" ? (System.Int64?)1 : null,
Dummy = "x"
})
group t by new { t.Dummy } into g
select new {
Category = g.Max(p => "Cleanliness Of Lounge"),
Excellent = g.Count(p => p.Column2 != null),
Very_Good = g.Count(p => p.Column3 != null),
Good = g.Count(p => p.Column4 != null),
Bad = g.Count(p => p.Column5 != null),
Poor = g.Count(p => p.Column6 != null)
}
).Union
( from t in
( from t in db.Feedbacks
where
t.DateOfVisit >= "2018-03-29" && t.DateOfVisit <= "2018-03-29"
select new {
Column1 = "Friendliness Of Staff",
Column2 =
t.FriendlinessOfStaff == "Excellent" ? (System.Int64?)1 : null,
Column3 =
t.FriendlinessOfStaff == "VeryGood" ? (System.Int64?)1 : null,
Column4 =
t.FriendlinessOfStaff == "Good" ? (System.Int64?)1 : null,
Column5 =
t.FriendlinessOfStaff == "Bad" ? (System.Int64?)1 : null,
Column6 =
t.FriendlinessOfStaff == "Poor" ? (System.Int64?)1 : null,
Dummy = "x"
})
group t by new { t.Dummy } into g
select new {
Category = g.Max(p => "Friendliness Of Staff"),
Excellent = g.Count(p => p.Column2 != null),
Very_Good = g.Count(p => p.Column3 != null),
Good = g.Count(p => p.Column4 != null),
Bad = g.Count(p => p.Column5 != null),
Poor = g.Count(p => p.Column6 != null)
}
).Union
( from t in
( from t in db.Feedbacks
where
t.DateOfVisit >= "2018-03-29" && t.DateOfVisit <= "2018-03-29"
select new {
Column1 = "Overall Experience",
Column2 =
t.OverAllExperience == "Excellent" ? (System.Int64?)1 : null,
Column3 =
t.OverAllExperience == "VeryGood" ? (System.Int64?)1 : null,
Column4 =
t.OverAllExperience == "Good" ? (System.Int64?)1 : null,
Column5 =
t.OverAllExperience == "Bad" ? (System.Int64?)1 : null,
Column6 =
t.OverAllExperience == "Poor" ? (System.Int64?)1 : null,
Dummy = "x"
})
group t by new { t.Dummy } into g
select new {
Category = g.Max(p => "Overall Experience"),
Excellent = g.Count(p => p.Column2 != null),
Very_Good = g.Count(p => p.Column3 != null),
Good = g.Count(p => p.Column4 != null),
Bad = g.Count(p => p.Column5 != null),
Poor = g.Count(p => p.Column6 != null)
}
)

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(+);

Providing a column name for the resultant table

I have the below query to extract data in desired format
select
(
select COUNT(serialNumber) as LOTQty
from MobileData
where mobileName = #mobileName and model = #model and LOTQty = #lotQty
)
,(
select COUNT(serialNumber) as FailedQty
from MobileData
where mobileName = #mobileName and model = #model and LOTQty = #lotQty and PreScan = 'FAIL'
)
But when I execute the query it certainly gives the desired result but instead of column names "LotQty" and "FailedQty" it display (No Column Name) and (No Column Name)
I also tried modifying the above query as below but still the result remains same
select
(
select [LotQty] = COUNT(serialNumber)
...
)
,(
select [FailedQty] = COUNT(serialNumber)
...
)
Any help is appreciated. I'm using sql server 2008
You can give colmn name using ALIAS and also you can fetch both count using one query instead of two subqueries.
Try this:
SELECT COUNT(serialNumber) AS LOTQty,
SUM(CASE WHEN PreScan = 'FAIL' THEN 1 ELSE 0 END) AS FailedQty
FROM MobileData
WHERE mobileName = #mobileName AND model = #model AND LOTQty = #lotQty;
The alias name must be outside the subquery
select
(
select COUNT(serialNumber)
from MobileData
where mobileName = #mobileName and model = #model and LOTQty = #lotQty
) as LOTQty
,(
select COUNT(serialNumber)
from MobileData
where mobileName = #mobileName and model = #model and LOTQty = #lotQty and PreScan = 'FAIL'
) as FailedQty

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

I need to check for the conditions at the bottom, but I cannot change use OR because of the (+) that are involved. I did not set that part up, so I am not sure how to rearrange it. Can someone tell me what I could do to get those conditions to go through?
SELECT DISTINCT
t.tkt_sid,
ts.tkt_sts_dsc,
tp.prty_dsc,
a.agt_cont_nbr,
au_a.user_nm assigned_to,
t.rcd_lst_user_ts
FROM
afp_asd.tkt t,
afp_asd.tkt_prty tp,
afp_asd.tkt_sts ts,
afp_asd.tkt_type tt,
afp_asd.tkt_log tl,
afp_asd.agt a,
afp_asd.asd_user au_a,
afp_asd.asd_user au_c,
afp_asd.asd_user au_l,
afp_asd.asd_user au_log,
afp_asd.prb_area pa1,
afp_asd.prb_area pa2,
afp_asd.prb_area pa3,
afp_asd.mktg_org mo,
afp_asd.src_sys ss,
afp_asd.agt ofc,
afp_asd.co c,
afp_asd.agt_sts ast
WHERE (
t.prty_cd = tp.prty_cd
AND t.tkt_sts_cd = ts.tkt_sts_cd
AND t.tkt_type_cd = tt.tkt_type_cd
AND t.agt_sid = a.agt_sid
AND t.assigned_id = au_a.asd_user_id
AND t.rcd_crt_user_id = au_c.asd_user_id (+)
AND t.lst_updater_id = au_l.asd_user_id
AND t.tkt_sid = tl.tkt_sid
AND t.prb_area_sid = pa3.prb_area_sid
AND tl.rcd_crt_user_id = au_log.asd_user_id
AND pa3.prb_hier_sid = pa2.prb_area_sid (+)
AND pa2.prb_hier_sid = pa1.prb_area_sid (+)
AND a.mktg_org_cd = mo.mktg_org_cd
AND a.src_sys_cd = mo.src_sys_cd
AND a.src_sys_cd = ss.src_sys_cd
AND a.ofc_id = ofc.agt_cont_nbr (+)
AND a.src_sys_cd = ofc.src_sys_cd (+)
AND a.co_sid = c.co_sid (+)
AND a.agt_sts_cd = ast.agt_sts_cd
AND tl.rcd_lst_user_ts >= :b_start_date
AND t.user_grp_sid = :b_group_id
)
AND (
(TKT_STS_DSC NOT LIKE 'Completed')
OR (
tp.prty_dsc = 'High'
AND ts.tkt_sts_dsc = 'Pending'
AND t.rcd_lst_user_ts < SYSDATE- 2
)
OR (
t.rcd_lst_user_ts < SYSDATE- 3
AND ts.tkt_sts_dsc = 'In Progress'
AND tp.prty_dsc = 'High'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 15
AND ts.tkt_sts_dsc = 'In Progress'
AND tp.prty_dsc = 'Medium'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 28
AND ts.tkt_sts_dsc = 'In Progress'
AND tp.prty_dsc = 'Low'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 7
AND ts.tkt_sts_dsc = 'Pending'
AND tp.prty_dsc = 'High'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 21
AND ts.tkt_sts_dsc = 'Pending'
AND tp.prty_dsc = 'Medium'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 43
AND ts.tkt_sts_dsc = 'Pending'
AND tp.prty_dsc = 'Low'
)
)
ORDER BY ASSIGNED_TO,
PRTY_DSC
Don't use the old FROM TableA,TableB WHERE ... join syntax. Just don't.
Instead, write out your joins individually:
FROM TableA
INNER JOIN TableB ON ...
LEFT JOIN TableC ON ...
This isn't just a general rant against the old syntax: using the new (I say "new", but it's more than 20 years old now), standard syntax will fix your problem in this case.