Numeric Value not reccognized - tsql
I am getting this error when INSERT data Numeric value 'Week 5 of Fiscal Month' is not recognized
I do not even have that field shown in the error message in my SQL
Please see the attached code. Without knowing what to look for i am posting here, atleast please point me to a subject that i need to look for
INSERT INTO FISCAL_WEEK_NEW (
FISCAL_WEEK_ID,
FISCAL_WEEK_DATE_ID,
FISCAL_MONTH_ID,
FISCAL_QUARTER_ID,
FISCAL_SEASON_ID,
FISCAL_YEAR_ID,
FIRST_DATE_IN_FISCAL_MONTH,
LAST_DATE_IN_FISCAL_MONTH,
FISCAL_MONTH_LAST_YEAR,
FISCAL_MONTH_2_YEAR_AGO,
FIRST_DATE_IN_FISCAL_QUARTER,
LAST_DATE_IN_FISCAL_QUARTER,
FISCAL_QUARTER_LAST_YEAR,
FISCAL_QUARTER_2_YEAR_AGO,
FIRST_DATE_IN_FISCAL_SEASON,
LAST_DATE_IN_FISCAL_SEASON ,
FISCAL_SEASON_LAST_YEAR,
FISCAL_SEASON_2_YEAR_AGO,
FIRST_DATE_IN_FISCAL_YEAR,
LAST_DATE_IN_FISCAL_YEAR,
FISCAL_YEAR_LAST_YEAR,
FISCAL_YEAR_2_YEAR_AGO,
FISCAL_WEEK_SEQUENCE_NUM,
FISCAL_WEEK_DISPLAY_NUM,
FISCAL_WEEK_DESC,
FISCAL_WEEK_SHORT_DESC,
FIRST_DATE_IN_FISCAL_WEEK,
LAST_DATE_IN_FISCAL_WEEK,
FISCAL_WEEK_ID_LAST_YEAR,
FISCAL_WEEK_ID_2_YEAR_AGO,
FISCAL_WEEK_ID_LAST_WEEK,
FISCAL_WEEK_ID_2_WEEK_AGO,
FISCAL_WEEK_ID_3_WEEK_AGO,
FISCAL_WEEK_ID_4_WEEK_AGO,
FISCAL_MONTH_SEQUENCE_NUM,
FISCAL_WEEK_IN_YEAR_NUM,
FISCAL_WEEK_IN_MONTH_NUM,
FISCAL_WEEK_IN_MONTH_NAME,
FISCAL_WEEK_IN_QUARTER_NUM,
FISCAL_WEEK_IN_QUARTER_NAME,
FISCAL_WEEK_CURRENT_IND,
LAST_COMPLETE_1_FISCAL_WEEK_IND,
LAST_COMPLETE_2_FISCAL_WEEK_IND,
LAST_COMPLETE_3_FISCAL_WEEK_IND,
LAST_COMPLETE_4_FISCAL_WEEK_IND,
LAST_COMPLETE_5_FISCAL_WEEK_IND,
LAST_COMPLETE_6_FISCAL_WEEK_IND,
LAST_COMPLETE_7_FISCAL_WEEK_IND,
LAST_COMPLETE_8_FISCAL_WEEK_IND,
LAST_COMPLETE_9_FISCAL_WEEK_IND,
LAST_COMPLETE_10_FISCAL_WEEK_IND,
LAST_COMPLETE_11_FISCAL_WEEK_IND,
LAST_COMPLETE_12_FISCAL_WEEK_IND,
FISCAL_WEEK_ID_5_WEEK_AGO,
FISCAL_WEEK_ID_6_WEEK_AGO,
FISCAL_WEEK_ID_7_WEEK_AGO,
FISCAL_WEEK_ID_8_WEEK_AGO,
FISCAL_WEEK_ID_9_WEEK_AGO,
FISCAL_WEEK_ID_10_WEEK_AGO,
FISCAL_WEEK_ID_11_WEEK_AGO,
FISCAL_WEEK_ID_12_WEEK_AGO,
FISCAL_WEEK_ID_13_WEEK_AGO,
FISCAL_WEEK_ID_14_WEEK_AGO,
FISCAL_WEEK_ID_15_WEEK_AGO,
FISCAL_WEEK_ID_16_WEEK_AGO,
FISCAL_WEEK_ID_17_WEEK_AGO,
FISCAL_WEEK_ID_18_WEEK_AGO,
FISCAL_WEEK_ID_19_WEEK_AGO,
FISCAL_WEEK_ID_20_WEEK_AGO,
FISCAL_WEEK_ID_21_WEEK_AGO,
FISCAL_WEEK_ID_22_WEEK_AGO,
FISCAL_WEEK_ID_23_WEEK_AGO,
FISCAL_WEEK_ID_24_WEEK_AGO,
FISCAL_WEEK_ID_25_WEEK_AGO,
FISCAL_WEEK_ID_26_WEEK_AGO,
FISCAL_WEEK_ID_27_WEEK_AGO,
FISCAL_WEEK_ID_28_WEEK_AGO,
FISCAL_WEEK_ID_29_WEEK_AGO,
FISCAL_WEEK_ID_30_WEEK_AGO,
FISCAL_WEEK_ID_31_WEEK_AGO,
FISCAL_WEEK_ID_32_WEEK_AGO,
FISCAL_WEEK_ID_33_WEEK_AGO,
FISCAL_WEEK_ID_34_WEEK_AGO,
FISCAL_WEEK_ID_35_WEEK_AGO,
FISCAL_WEEK_ID_36_WEEK_AGO,
FISCAL_WEEK_ID_37_WEEK_AGO,
FISCAL_WEEK_ID_38_WEEK_AGO,
FISCAL_WEEK_ID_39_WEEK_AGO,
FISCAL_WEEK_ID_40_WEEK_AGO,
FISCAL_WEEK_ID_41_WEEK_AGO,
FISCAL_WEEK_ID_42_WEEK_AGO,
FISCAL_WEEK_ID_43_WEEK_AGO,
FISCAL_WEEK_ID_44_WEEK_AGO,
FISCAL_WEEK_ID_45_WEEK_AGO,
FISCAL_WEEK_ID_46_WEEK_AGO,
FISCAL_WEEK_ID_47_WEEK_AGO,
FISCAL_WEEK_ID_48_WEEK_AGO,
FISCAL_WEEK_ID_49_WEEK_AGO,
FISCAL_WEEK_ID_50_WEEK_AGO,
FISCAL_WEEK_ID_51_WEEK_AGO,
FISCAL_WEEK_ID_52_WEEK_AGO,
FISCAL_WEEK_ID_3_YEAR_AGO,
FISCAL_WEEK_ID_4_YEAR_AGO,
FISCAL_WEEK_ID_5_YEAR_AGO,
FISCAL_WEEK_ID_6_YEAR_AGO,
FISCAL_WEEK_ID_7_YEAR_AGO,
FISCAL_WEEK_ID_8_YEAR_AGO,
FISCAL_WEEK_ID_9_YEAR_AGO,
FISCAL_WEEK_ID_10_YEAR_AGO,
FISCAL_WEEK_ID_1_QUARTER_AGO,
FISCAL_WEEK_ID_2_QUARTER_AGO,
FISCAL_WEEK_ID_3_QUARTER_AGO,
FISCAL_WEEK_ID_4_QUARTER_AGO,
FISCAL_WEEK_ID_5_QUARTER_AGO,
FISCAL_WEEK_ID_6_QUARTER_AGO,
FISCAL_WEEK_ID_7_QUARTER_AGO,
FISCAL_WEEK_ID_8_QUARTER_AGO,
FISCAL_WEEK_ID_9_QUARTER_AGO,
FISCAL_WEEK_ID_10_QUARTER_AGO,
FISCAL_WEEK_ID_1_MONTH_AGO,
FISCAL_WEEK_ID_2_MONTH_AGO,
FISCAL_WEEK_ID_3_MONTH_AGO,
FISCAL_WEEK_ID_4_MONTH_AGO,
FISCAL_WEEK_ID_5_MONTH_AGO,
FISCAL_WEEK_ID_6_MONTH_AGO,
FISCAL_WEEK_ID_7_MONTH_AGO,
FISCAL_WEEK_ID_8_MONTH_AGO,
FISCAL_WEEK_ID_9_MONTH_AGO,
FISCAL_WEEK_ID_10_MONTH_AGO,
FISCAL_WEEK_ID_11_MONTH_AGO,
FISCAL_WEEK_ID_12_MONTH_AGO,
FISCAL_WEEK_ID_13_MONTH_AGO,
FISCAL_WEEK_ID_LAST_YEAR_2_WEEK_AGO,
FISCAL_WEEK_ID_LAST_YEAR_3_WEEK_AGO,
FISCAL_WEEK_ID_LAST_YEAR_4_WEEK_AGO,
FISCAL_WEEK_ID_LAST_YEAR_5_WEEK_AGO,
CREATE_PROCESS_CD,
CREATE_USER,
CREATE_TS,
UPDATE_PROCESS_CD,
UPDATE_USER,
UPDATE_TS
)
with cte as
(
SELECT DISTINCT A.fiscal_week_id,
A.fiscal_week_date_id,
A.fiscal_month_id,
A.fiscal_quarter_id,
A.fiscal_season_id,
A.fiscal_year_id,
a.first_date_in_fiscal_month,
A.last_date_in_fiscal_month,
A.fiscal_month_last_year,
A.fiscal_month_2_year_ago,
A.first_date_in_fiscal_quarter,
A.last_date_in_fiscal_quarter,
A.fiscal_quarter_last_year,
A.fiscal_quarter_2_year_ago,
A.first_date_in_fiscal_season,
A.last_date_in_fiscal_season,
A.fiscal_season_last_year,
A.fiscal_season_2_year_ago,
A.first_date_in_fiscal_year,
A.last_date_in_fiscal_year,
A.fiscal_year_last_year,
A.fiscal_year_2_year_ago,
A.fiscal_week_sequence_num,
A.fiscal_week_display_num,
A.fiscal_week_desc,
A.fiscal_week_short_desc,
A.first_date_in_fiscal_week,
A.last_date_in_fiscal_week,
A.fiscal_month_sequence_num,
A.fiscal_week_in_year_num,
A.fiscal_week_in_month_num,
A.fiscal_week_in_month_name,
A.fiscal_week_in_quarter_num,
A.fiscal_week_in_quarter_name,
A.fiscal_week_current_ind,
A.last_complete_1_fiscal_week_ind,
A.last_complete_2_fiscal_week_ind,
A.last_complete_3_fiscal_week_ind,
A.last_complete_4_fiscal_week_ind,
A.last_complete_5_fiscal_week_ind,
A.last_complete_6_fiscal_week_ind,
A.last_complete_7_fiscal_week_ind,
A.last_complete_8_fiscal_week_ind,
A.last_complete_9_fiscal_week_ind,
A.last_complete_10_fiscal_week_ind,
A.last_complete_11_fiscal_week_ind,
A.last_complete_12_fiscal_week_ind,
COALESCE(LW1.fiscal_week_id, 0) fiscal_week_id_last_week,
COALESCE(LW2.fiscal_week_id, 0) fiscal_week_id_2_week_ago,
COALESCE(LW3.fiscal_week_id, 0) fiscal_week_id_3_week_ago,
COALESCE(LW4.fiscal_week_id, 0) fiscal_week_id_4_week_ago,
COALESCE(LW5.fiscal_week_id, 0) fiscal_week_id_5_week_ago,
COALESCE(LW6.fiscal_week_id, 0) fiscal_week_id_6_week_ago,
COALESCE(LW7.fiscal_week_id, 0) fiscal_week_id_7_week_ago,
COALESCE(LW8.fiscal_week_id, 0) fiscal_week_id_8_week_ago,
COALESCE(LW9.fiscal_week_id, 0) fiscal_week_id_9_week_ago,
COALESCE(LW10.fiscal_week_id, 0) fiscal_week_id_10_week_ago,
COALESCE(LW11.fiscal_week_id, 0) fiscal_week_id_11_week_ago,
COALESCE(LW12.fiscal_week_id, 0) fiscal_week_id_12_week_ago,
COALESCE(LW13.fiscal_week_id, 0) fiscal_week_id_13_week_ago,
COALESCE(LW14.fiscal_week_id, 0) fiscal_week_id_14_week_ago,
COALESCE(LW15.fiscal_week_id, 0) fiscal_week_id_15_week_ago,
COALESCE(LW16.fiscal_week_id, 0) fiscal_week_id_16_week_ago,
COALESCE(LW17.fiscal_week_id, 0) fiscal_week_id_17_week_ago,
COALESCE(LW18.fiscal_week_id, 0) fiscal_week_id_18_week_ago,
COALESCE(LW19.fiscal_week_id, 0) fiscal_week_id_19_week_ago,
COALESCE(LW20.fiscal_week_id, 0) fiscal_week_id_20_week_ago,
COALESCE(LW21.fiscal_week_id, 0) fiscal_week_id_21_week_ago,
COALESCE(LW22.fiscal_week_id, 0) fiscal_week_id_22_week_ago,
COALESCE(LW23.fiscal_week_id, 0) fiscal_week_id_23_week_ago,
COALESCE(LW24.fiscal_week_id, 0) fiscal_week_id_24_week_ago,
COALESCE(LW25.fiscal_week_id, 0) fiscal_week_id_25_week_ago,
COALESCE(LW26.fiscal_week_id, 0) fiscal_week_id_26_week_ago,
COALESCE(LW27.fiscal_week_id, 0) fiscal_week_id_27_week_ago,
COALESCE(LW28.fiscal_week_id, 0) fiscal_week_id_28_week_ago,
COALESCE(LW29.fiscal_week_id, 0) fiscal_week_id_29_week_ago,
COALESCE(LW30.fiscal_week_id, 0) fiscal_week_id_30_week_ago,
COALESCE(LW31.fiscal_week_id, 0) fiscal_week_id_31_week_ago,
COALESCE(LW32.fiscal_week_id, 0) fiscal_week_id_32_week_ago,
COALESCE(LW33.fiscal_week_id, 0) fiscal_week_id_33_week_ago,
COALESCE(LW34.fiscal_week_id, 0) fiscal_week_id_34_week_ago,
COALESCE(LW35.fiscal_week_id, 0) fiscal_week_id_35_week_ago,
COALESCE(LW36.fiscal_week_id, 0) fiscal_week_id_36_week_ago,
COALESCE(LW37.fiscal_week_id, 0) fiscal_week_id_37_week_ago,
COALESCE(LW38.fiscal_week_id, 0) fiscal_week_id_38_week_ago,
COALESCE(LW39.fiscal_week_id, 0) fiscal_week_id_39_week_ago,
COALESCE(LW40.fiscal_week_id, 0) fiscal_week_id_40_week_ago,
COALESCE(LW41.fiscal_week_id, 0) fiscal_week_id_41_week_ago,
COALESCE(LW42.fiscal_week_id, 0) fiscal_week_id_42_week_ago,
COALESCE(LW43.fiscal_week_id, 0) fiscal_week_id_43_week_ago,
COALESCE(LW44.fiscal_week_id, 0) fiscal_week_id_44_week_ago,
COALESCE(LW45.fiscal_week_id, 0) fiscal_week_id_45_week_ago,
COALESCE(LW46.fiscal_week_id, 0) fiscal_week_id_46_week_ago,
COALESCE(LW47.fiscal_week_id, 0) fiscal_week_id_47_week_ago,
COALESCE(LW48.fiscal_week_id, 0) fiscal_week_id_48_week_ago,
COALESCE(LW49.fiscal_week_id, 0) fiscal_week_id_49_week_ago,
COALESCE(LW50.fiscal_week_id, 0) fiscal_week_id_50_week_ago,
COALESCE(LW51.fiscal_week_id, 0) fiscal_week_id_51_week_ago,
COALESCE(LW52.fiscal_week_id, 0) fiscal_week_id_52_week_ago,
COALESCE(B.fiscal_week_id, 0) FISCAL_WEEK_ID_LAST_YEAR,
/*FWIDLY*/
COALESCE(C.fiscal_week_id, 0) FISCAL_WEEK_ID_2_YEAR_AGO,
/*FWID2Y*/
COALESCE(D.fiscal_week_id, 0) FISCAL_WEEK_ID_3_YEAR_AGO,
/* FWI3Y*/
COALESCE(E.fiscal_week_id, 0) FISCAL_WEEK_ID_4_YEAR_AGO,
/* FWI4Y*/
COALESCE(F.fiscal_week_id, 0) FISCAL_WEEK_ID_5_YEAR_AGO,
/* FWI5Y*/
COALESCE(G.fiscal_week_id, 0) FISCAL_WEEK_ID_6_YEAR_AGO,
/* FWI6Y*/
COALESCE(H.fiscal_week_id, 0) FISCAL_WEEK_ID_7_YEAR_AGO,
/* FWI7Y*/
COALESCE(I.fiscal_week_id, 0) FISCAL_WEEK_ID_8_YEAR_AGO,
/* FWI8Y*/
COALESCE(J.fiscal_week_id, 0) FISCAL_WEEK_ID_9_YEAR_AGO,
/* FWI9Y*/
COALESCE(K.fiscal_week_id, 0) FISCAL_WEEK_ID_10_YEAR_AGO,
/* FWI10Y,*/
----COALESCE(LW52.fiscal_week_id,0) fiscal_week_id_2_week_ago,
COALESCE(LQ1.fiscal_week_id, 0) fiscal_week_id_1_quarter_ago,
COALESCE(LQ2.fiscal_week_id, 0) fiscal_week_id_2_quarter_ago,
COALESCE(LQ3.fiscal_week_id, 0) fiscal_week_id_3_quarter_ago,
COALESCE(LQ4.fiscal_week_id, 0) fiscal_week_id_4_quarter_ago,
COALESCE(LQ5.fiscal_week_id, 0) fiscal_week_id_5_quarter_ago,
COALESCE(LQ6.fiscal_week_id, 0) fiscal_week_id_6_quarter_ago,
COALESCE(LQ7.fiscal_week_id, 0) fiscal_week_id_7_quarter_ago,
COALESCE(LQ8.fiscal_week_id, 0) fiscal_week_id_8_quarter_ago,
COALESCE(LQ9.fiscal_week_id, 0) fiscal_week_id_9_quarter_ago,
COALESCE(LQ10.fiscal_week_id, 0) fiscal_week_id_10_quarter_ago,
COALESCE(LM1.fiscal_week_id, 0) fiscal_week_id_1_month_ago,
COALESCE(LM2.fiscal_week_id, 0) fiscal_week_id_2_month_ago,
COALESCE(LM3.fiscal_week_id, 0) fiscal_week_id_3_month_ago,
COALESCE(LM4.fiscal_week_id, 0) fiscal_week_id_4_month_ago,
COALESCE(LM5.fiscal_week_id, 0) fiscal_week_id_5_month_ago,
COALESCE(LM6.fiscal_week_id, 0) fiscal_week_id_6_month_ago,
COALESCE(LM7.fiscal_week_id, 0) fiscal_week_id_7_month_ago,
COALESCE(LM8.fiscal_week_id, 0) fiscal_week_id_8_month_ago,
COALESCE(LM9.fiscal_week_id, 0) fiscal_week_id_9_month_ago,
COALESCE(LM10.fiscal_week_id, 0) fiscal_week_id_10_month_ago,
COALESCE(LM11.fiscal_week_id, 0) fiscal_week_id_11_month_ago,
COALESCE(LM12.fiscal_week_id, 0) fiscal_week_id_12_month_ago,
COALESCE(LM13.fiscal_week_id, 0) fiscal_week_id_13_month_ago,
COALESCE(LY2W.fiscal_week_id, 0)
fiscal_week_id_last_year_2_week_ago,
COALESCE(LY3W.fiscal_week_id, 0)
fiscal_week_id_last_year_3_week_ago,
COALESCE(LY4W.fiscal_week_id, 0)
fiscal_week_id_last_year_4_week_ago,
COALESCE(LY5W.fiscal_week_id, 0)
fiscal_week_id_last_year_5_week_ago,
'SQL_SCRIPT' AS CREATE_PROCESS_CD
,'BKANDALA' AS CREATE_USER
,CURRENT_TIMESTAMP AS CREATE_TS
,NULL AS UPDATE_PROCESS_CD
,NULL AS UPDATE_USER
,NULL AS UPDATE_TS
FROM fiscal_week A
LEFT OUTER JOIN fiscal_week B
ON A.fiscal_week_id_last_year = B.fiscal_week_id
AND A.fiscal_week_id > B.fiscal_week_id
LEFT OUTER JOIN fiscal_week C
ON A.fiscal_week_id_2_year_ago = C.fiscal_week_id
LEFT OUTER JOIN fiscal_week D
ON A.fiscal_week_id_3_year_ago = D.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week E
ON A.fiscal_week_id_4_year_ago = E.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week F
ON A.fiscal_week_id_5_year_ago = F.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week G
ON A.fiscal_week_id_6_year_ago = G.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week H
ON A.fiscal_week_id_7_year_ago = H.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week I
ON A.fiscal_week_id_8_year_ago = I.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week J
ON A.fiscal_week_id_9_year_ago = J.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week K
ON A.fiscal_week_id_10_year_ago = K.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW1
ON A.fiscal_week_id_last_week = LW1.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW2
ON
A.fiscal_week_id_2_week_ago = LW2.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW3
ON
A.fiscal_week_id_3_week_ago = LW3.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW4
ON
A.fiscal_week_id_4_week_ago = LW4.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW5
ON
A.fiscal_week_id_5_week_ago = LW5.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW6
ON
A.fiscal_week_id_6_week_ago = LW6.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW7
ON
A.fiscal_week_id_7_week_ago = LW7.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW8
ON
A.fiscal_week_id_8_week_ago = LW8.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW9
ON
A.fiscal_week_id_9_week_ago = LW9.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW10
ON
A.fiscal_week_id_10_week_ago = LW10.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW11
ON
A.fiscal_week_id_11_week_ago = LW11.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW12
ON
A.fiscal_week_id_12_week_ago = LW12.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW13
ON
A.fiscal_week_id_13_week_ago = LW13.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW14
ON
A.fiscal_week_id_14_week_ago = LW14.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW15
ON
A.fiscal_week_id_15_week_ago = LW15.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW16
ON
A.fiscal_week_id_16_week_ago = LW16.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW17
ON
A.fiscal_week_id_17_week_ago = LW17.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW18
ON
A.fiscal_week_id_18_week_ago = LW18.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW19
ON
A.fiscal_week_id_19_week_ago = LW19.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW20
ON
A.fiscal_week_id_20_week_ago = LW20.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW21
ON
A.fiscal_week_id_21_week_ago = LW21.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW22
ON
A.fiscal_week_id_22_week_ago = LW22.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW23
ON
A.fiscal_week_id_23_week_ago = LW23.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW24
ON
A.fiscal_week_id_24_week_ago = LW24.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW25
ON
A.fiscal_week_id_25_week_ago = LW25.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW26
ON
A.fiscal_week_id_26_week_ago = LW26.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW27
ON
A.fiscal_week_id_27_week_ago = LW27.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW28
ON
A.fiscal_week_id_28_week_ago = LW28.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW29
ON
A.fiscal_week_id_29_week_ago = LW29.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW30
ON
A.fiscal_week_id_30_week_ago = LW30.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW31
ON
A.fiscal_week_id_31_week_ago = LW31.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW32
ON
A.fiscal_week_id_32_week_ago = LW32.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW33
ON
A.fiscal_week_id_33_week_ago = LW33.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW34
ON
A.fiscal_week_id_34_week_ago = LW34.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW35
ON
A.fiscal_week_id_35_week_ago = LW35.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW36
ON
A.fiscal_week_id_36_week_ago = LW36.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW37
ON
A.fiscal_week_id_37_week_ago = LW37.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW38
ON
A.fiscal_week_id_38_week_ago = LW38.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW39
ON
A.fiscal_week_id_39_week_ago = LW39.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW40
ON
A.fiscal_week_id_40_week_ago = LW40.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW41
ON
A.fiscal_week_id_41_week_ago = LW41.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW42
ON
A.fiscal_week_id_42_week_ago = LW42.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW43
ON
A.fiscal_week_id_43_week_ago = LW43.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW44
ON
A.fiscal_week_id_44_week_ago = LW44.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW45
ON
A.fiscal_week_id_45_week_ago = LW45.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW46
ON
A.fiscal_week_id_46_week_ago = LW46.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW47
ON
A.fiscal_week_id_47_week_ago = LW47.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW48
ON
A.fiscal_week_id_48_week_ago = LW48.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW49
ON
A.fiscal_week_id_49_week_ago = LW49.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW50
ON
A.fiscal_week_id_50_week_ago = LW50.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW51
ON
A.fiscal_week_id_51_week_ago = LW51.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LW52
ON
A.fiscal_week_id_52_week_ago = LW52.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LQ1
ON
A.fiscal_week_id_1_quarter_ago = LQ1.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LQ2
ON
A.fiscal_week_id_2_quarter_ago = LQ2.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LQ3
ON
A.fiscal_week_id_3_quarter_ago = LQ3.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LQ4
ON
A.fiscal_week_id_4_quarter_ago = LQ4.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LQ5
ON
Dateadd(day, -10, A.fiscal_week_id_5_quarter_ago) = LQ5.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LQ6
ON A.fiscal_week_id_6_quarter_ago = LQ6.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LQ7
ON A.fiscal_week_id_7_quarter_ago = LQ7.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LQ8
ON A.fiscal_week_id_8_quarter_ago = LQ8.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LQ9
ON A.fiscal_week_id_9_quarter_ago = LQ9.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LQ10
ON A.fiscal_week_id_10_quarter_ago = LQ10.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LM1
ON A.fiscal_week_id_1_month_ago = LM1.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LM2
ON A.fiscal_week_id_2_month_ago = LM2.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LM3
ON A.fiscal_week_id_3_month_ago = LM3.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LM4
ON A.fiscal_week_id_4_month_ago = LM4.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LM5
ON A.fiscal_week_id_5_month_ago = LM5.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LM6
ON A.fiscal_week_id_6_month_ago = LM6.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LM7
ON A.fiscal_week_id_7_month_ago = LM7.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LM8
ON A.fiscal_week_id_8_month_ago = LM8.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LM9
ON A.fiscal_week_id_9_month_ago = LM9.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LM10
ON A.fiscal_week_id_10_month_ago = LM10.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LM11
ON A.fiscal_week_id_11_month_ago = LM11.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LM12
ON A.fiscal_week_id_12_month_ago = LM12.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LM13
ON A.fiscal_week_id_13_month_ago = LM13.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LY2W
ON
A.fiscal_week_id_last_year_2_week_ago = LY2W.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LY3W
ON
A.fiscal_week_id_last_year_3_week_ago = LY3W.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LY4W
ON
A.fiscal_week_id_last_year_4_week_ago = LY4W.last_date_in_fiscal_week
LEFT OUTER JOIN fiscal_week LY5W
ON
A.fiscal_week_id_last_year_5_week_ago = LY5W.last_date_in_fiscal_week
)
select *
from cte;
----COMMIT;
Your data is getting shifted due to delimiter try identifying the column where "Week 5 of Fiscal Month" is getting inserted .To validate the simple option would be to create temporary table with all column as string and then try inserting the record
Related
Jasper Report: error executing SQL statement by Collection Parameter
I'm creating a report which's passing by collection parameter. The parameter name as 'ids' and the class is java.util.Collection. My query: select * from order_deliveryorder a left join order_deliveryorderline b on a.id = b.d_order_id left join order_item c on c.id = b.product_id left join order_orderline e on e.id = b.s_order_id left join order_order f on e.order_id = f.id left join order_itemsection g on g.id = f.section_id left join order_location d on d.id = a.location_id left join order_location h on h.id = a.from_location_id where a.id in ('2377900603251741014','2377900603251740997','2377900603251740967') the query in jasper: select * from order_deliveryorder a left join order_deliveryorderline b on a.id = b.d_order_id left join order_item c on c.id = b.product_id left join order_orderline e on e.id = b.s_order_id left join order_order f on e.order_id = f.id left join order_itemsection g on g.id = f.section_id left join order_location d on d.id = a.location_id left join order_location h on h.id = a.from_location_id where $X{IN, a.id, ids} The value of parameter "ids" is ["2377900603251741014","2377900603251740997","2377900603251740967"] In the End, had met the error executing sql statement.
Order dates with multiple columns (some containing nulls)
I have 4 dates (derived from separate subqueries) as in below but I want to find the MAX and MIN dates of the values returned. I've tried using CASE which will not work with the null values. Also tried using COALESCE in the subs to get values and then using CTE but this also didn't work. (The overall query is to find customers with no -9 in a1.id) There's a lot of help in SO but nothing that I can get to fit this purpose. Any help would be greatly appreciated. DESIRED COLUMNS PAT_ID Date1 Date2 Date3 Date4 MAX MIN 1 01/04/2015 25/12/2000 02/02/2011 01/04/2015 25/12/2000 2 05/08/1950 11/11/2011 11/11/2011 05/08/1950 3 04/01/1958 04/01/1958 04/01/1958 4 01/01/1900 01/01/1900 5 15/08/2017 07/07/1967 15/08/2017 07/07/1967 select distinct ca1.id AS PT_ID, (select Top 1 s1z.appointmen ApptDate from schl_booking_appoin s1z LEFT OUTER JOIN core_patient c3z ON s1z.patient = c3z.id LEFT OUTER JOIN schl_appt_history_s s2z ON s1z.currentsta = s2z.id LEFT OUTER JOIN applookup_instance a2z ON s2z.lkp_status = a2z.id where (c3z.id = ca1.id and a2z.text not like '%cancelled%' and s1z.appointmen > :SYS_DATE_TIME) order by s1z.appointmen asc ) AS Date1, (select Top 1 s1q.appointmen ApptDate from schl_booking_appoin s1q LEFT OUTER JOIN core_patient c3q ON s1q.patient = c3q.id LEFT OUTER JOIN schl_appt_history_s s2q ON s1q.currentsta = s2q.id LEFT OUTER JOIN applookup_instance a2q ON s2q.lkp_status = a2q.id where (c3q.id = ca1.id and a2q.text not like '%cancelled%' and s1q.appointmen < :SYS_DATE_TIME) order by s1q.appointmen desc ) AS Date2, (select top 1 CONVERT (VARCHAR(20),c2y.admissiond, 103) AdmissionDate from core_admissiondetai c2y LEFT OUTER JOIN core_pas_event c3y ON c2y.pasevent = c3y.id LEFT OUTER JOIN core_patient c4y ON c3y.patient = c4y.id where (c4y.id = ca1.id) order by c2y.admissiond desc ) AS Date3, (select top 1 CONVERT(VARCHAR(20),c5k.arrivaldat,103) ArrivalDate from core_emergencyatten c5k LEFT OUTER JOIN core_patient c6k ON c5k.patient = c6k.id where (c6k.id = ca1.id) order by c5k.arrivaldat desc ) AS Date4 from core_patient ca1 LEFT OUTER JOIN core_patient_c_identifi ca2 ON ca1.id = ca2.id LEFT OUTER JOIN applookup_instance aa2 ON ca1.lkp_sex = aa2.id LEFT OUTER JOIN applookup_instance aa1 ON ca2.lkp_c_ty = aa1.id LEFT OUTER JOIN applookup_instance ab1 ON ca1.lkp_overseascl = ab1.id LEFT OUTER JOIN core_organisation cx4 ON ca1.practice = cx4.id WHERE ca1.id IN ( select c1.id core_patient_c1_id from core_patient c1 LEFT OUTER JOIN core_patient_c_identifi c2 ON c1.id = c2.id LEFT OUTER JOIN applookup_instance a1 ON c2.lkp_c_ty = a1.id where ((ca2.merged = 0 or ca2.merged is null) and aa1.id = -2404) ) AND ca1.id NOT IN ( select c1.id core_patient_c1_id from core_patient c1 LEFT OUTER JOIN core_patient_c_identifi c2 ON c1.id = c2.id LEFT OUTER JOIN applookup_instance a1 ON c2.lkp_c_ty = a1.id where (c1.namesurname not like '%XXTEST%' and c1.namesurname not like '%ZZ%' and a1.id = -9) )
try this version. Your old query acts as a subquery and introducing a new column definition to get the Max Date. SELECT PT_ID, ( SELECT Max(v) FROM (VALUES (Date1), (Date2), (Date3),(Date4)) AS value(v) ) as [MaxDate] FROM ( select distinct ca1.id AS PT_ID, (select Top 1 s1z.appointmen ApptDate from schl_booking_appoin s1z LEFT OUTER JOIN core_patient c3z ON s1z.patient = c3z.id LEFT OUTER JOIN schl_appt_history_s s2z ON s1z.currentsta = s2z.id LEFT OUTER JOIN applookup_instance a2z ON s2z.lkp_status = a2z.id where (c3z.id = ca1.id and a2z.text not like '%cancelled%' and s1z.appointmen > :SYS_DATE_TIME) order by s1z.appointmen asc ) AS Date1, (select Top 1 s1q.appointmen ApptDate from schl_booking_appoin s1q LEFT OUTER JOIN core_patient c3q ON s1q.patient = c3q.id LEFT OUTER JOIN schl_appt_history_s s2q ON s1q.currentsta = s2q.id LEFT OUTER JOIN applookup_instance a2q ON s2q.lkp_status = a2q.id where (c3q.id = ca1.id and a2q.text not like '%cancelled%' and s1q.appointmen < :SYS_DATE_TIME) order by s1q.appointmen desc ) AS Date2, (select top 1 CONVERT (VARCHAR(20),c2y.admissiond, 103) AdmissionDate from core_admissiondetai c2y LEFT OUTER JOIN core_pas_event c3y ON c2y.pasevent = c3y.id LEFT OUTER JOIN core_patient c4y ON c3y.patient = c4y.id where (c4y.id = ca1.id) order by c2y.admissiond desc ) AS Date3, (select top 1 CONVERT(VARCHAR(20),c5k.arrivaldat,103) ArrivalDate from core_emergencyatten c5k LEFT OUTER JOIN core_patient c6k ON c5k.patient = c6k.id where (c6k.id = ca1.id) order by c5k.arrivaldat desc ) AS Date4 from core_patient ca1 LEFT OUTER JOIN core_patient_c_identifi ca2 ON ca1.id = ca2.id LEFT OUTER JOIN applookup_instance aa2 ON ca1.lkp_sex = aa2.id LEFT OUTER JOIN applookup_instance aa1 ON ca2.lkp_c_ty = aa1.id LEFT OUTER JOIN applookup_instance ab1 ON ca1.lkp_overseascl = ab1.id LEFT OUTER JOIN core_organisation cx4 ON ca1.practice = cx4.id WHERE ca1.id IN ( select c1.id core_patient_c1_id from core_patient c1 LEFT OUTER JOIN core_patient_c_identifi c2 ON c1.id = c2.id LEFT OUTER JOIN applookup_instance a1 ON c2.lkp_c_ty = a1.id where ((ca2.merged = 0 or ca2.merged is null) and aa1.id = -2404) ) AND ca1.id NOT IN ( select c1.id core_patient_c1_id from core_patient c1 LEFT OUTER JOIN core_patient_c_identifi c2 ON c1.id = c2.id LEFT OUTER JOIN applookup_instance a1 ON c2.lkp_c_ty = a1.id where (c1.namesurname not like '%XXTEST%' and c1.namesurname not like '%ZZ%' and a1.id = -9) ) )Sub
You could do the following: make your query into a CTE; flatten the data so it becomes a list of PT_IDs and dates; from this flattened data find the maximum and minimum date per PT_ID; bring this all back together in one final query. This is a stab at doing all of the above: WITH x AS ( select distinct ca1.id AS PT_ID, (select Top 1 s1z.appointmen ApptDate from schl_booking_appoin s1z LEFT OUTER JOIN core_patient c3z ON s1z.patient = c3z.id LEFT OUTER JOIN schl_appt_history_s s2z ON s1z.currentsta = s2z.id LEFT OUTER JOIN applookup_instance a2z ON s2z.lkp_status = a2z.id where (c3z.id = ca1.id and a2z.text not like '%cancelled%' and s1z.appointmen > :SYS_DATE_TIME) order by s1z.appointmen asc ) AS Date1, (select Top 1 s1q.appointmen ApptDate from schl_booking_appoin s1q LEFT OUTER JOIN core_patient c3q ON s1q.patient = c3q.id LEFT OUTER JOIN schl_appt_history_s s2q ON s1q.currentsta = s2q.id LEFT OUTER JOIN applookup_instance a2q ON s2q.lkp_status = a2q.id where (c3q.id = ca1.id and a2q.text not like '%cancelled%' and s1q.appointmen < :SYS_DATE_TIME) order by s1q.appointmen desc ) AS Date2, (select top 1 CONVERT (VARCHAR(20),c2y.admissiond, 103) AdmissionDate from core_admissiondetai c2y LEFT OUTER JOIN core_pas_event c3y ON c2y.pasevent = c3y.id LEFT OUTER JOIN core_patient c4y ON c3y.patient = c4y.id where (c4y.id = ca1.id) order by c2y.admissiond desc ) AS Date3, (select top 1 CONVERT(VARCHAR(20),c5k.arrivaldat,103) ArrivalDate from core_emergencyatten c5k LEFT OUTER JOIN core_patient c6k ON c5k.patient = c6k.id where (c6k.id = ca1.id) order by c5k.arrivaldat desc ) AS Date4 from core_patient ca1 LEFT OUTER JOIN core_patient_c_identifi ca2 ON ca1.id = ca2.id LEFT OUTER JOIN applookup_instance aa2 ON ca1.lkp_sex = aa2.id LEFT OUTER JOIN applookup_instance aa1 ON ca2.lkp_c_ty = aa1.id LEFT OUTER JOIN applookup_instance ab1 ON ca1.lkp_overseascl = ab1.id LEFT OUTER JOIN core_organisation cx4 ON ca1.practice = cx4.id WHERE ca1.id IN ( select c1.id core_patient_c1_id from core_patient c1 LEFT OUTER JOIN core_patient_c_identifi c2 ON c1.id = c2.id LEFT OUTER JOIN applookup_instance a1 ON c2.lkp_c_ty = a1.id where ((ca2.merged = 0 or ca2.merged is null) and aa1.id = -2404) ) AND ca1.id NOT IN ( select c1.id core_patient_c1_id from core_patient c1 LEFT OUTER JOIN core_patient_c_identifi c2 ON c1.id = c2.id LEFT OUTER JOIN applookup_instance a1 ON c2.lkp_c_ty = a1.id where (c1.namesurname not like '%XXTEST%' and c1.namesurname not like '%ZZ%' and a1.id = -9) )), y AS ( SELECT PT_ID, Date1 AS [Date] FROM x WHERE Date1 IS NOT NULL UNION ALL SELECT PT_ID, Date2 AS [Date] FROM x WHERE Date2 IS NOT NULL UNION ALL SELECT PT_ID, Date3 AS [Date] FROM x WHERE Date3 IS NOT NULL UNION ALL SELECT PT_ID, Date4 AS [Date] FROM x WHERE Date4 IS NOT NULL), z AS ( SELECT PT_ID, MIN([Date]) AS Min_Date, MAX([Date]) AS Max_Date FROM y GROUP BY PT_ID) SELECT x.PT_ID, x.Date1, x.Date2, x.Date3, x.Date4, z.Min_Date, z.Max_Date FROM x LEFT JOIN z ON z.PT_ID = x.PT_ID;
Need to replace case statement in where clause
**The below query is working badly, and I'm planning to replace case stmt in where clause with temp table. Pls suggest to me some option to rewrite this query Is there a way to simplify this into multiple SQLs Also explain me how this case stmt works in where clause** select DISTINCT ph.order_type_cd, c.order_num, ph.created_date, os.order_status_desc, ph.facility_name, PH.vendor_order_num, l.lic_nm, (u.last_nm + ', ' + u.first_nm )as requestor_nm, bl.billing_location_desc, ph.ship_to_name, dm.dispatch_method_name_desc as courier, PH.po_last_updated_dt as last_updated_date, (u1.last_nm + ', ' + u1.first_nm )as last_updated_by, ph.right_grp_id, c.line_id, a.material_product_id, a.parent_material_product_id from avt_po_detail c inner join avt_po_hdr ph on ph.order_num = c.order_num left outer join avt_material_product AS a on a.material_product_id=c.material_product_id INNER JOIN #entity_ids po on po.entity_id = c.order_num --or #select_pos = 0 ) LEFT OUTER JOIN dbo.ert_product AS pc ON pc.product_id = a.product_id left outer join ert_product_type as pt on pt.product_type_cd = pc.product_type_cd LEFT OUTER JOIN dbo.ert_product_xref as xref on xref.product_id = a.parent_product_id AND xref.version_id = 1 left outer join avt_order_status AS OS ON OS.order_status_cd = PH.order_status_cd left outer join avt_licensee l on l.lic_id = ph.lic_id left outer join avt_po_from pf on pf.material_product_id = a.material_product_id and pf.line_id = c.line_id left outer join avt_po_audio_detail pad on pad.line_id = c.line_id left outer join avt_materials_trans_material MTM on mtm.sequence_no = c.video_seq_no left outer join avt_user u on u.userid = ph.requestor_id left outer join avt_user u1 on u1.userid = ph.po_last_updated_by left outer join avt_billing_location bl on bl.billing_location_cd = PH.billing_location_cd left outer join avt_dispatch_method_name dm on dm.dispatch_method_name_id = PH.dispatch_method_name_id left outer join avt_right_group arg on arg.right_grp_id = ph.right_grp_id left outer join #prod product on product.product_id=a.product_id and (product.version_id=c.dist_version_id OR product.version_id = 1) where (pf.from_desc in ( select from_desc from #from_desc ) or #select_form = 0) and (case WHEN c.dist_version_id IS NULL THEN c.UCS_prod_no ELSE CASE WHEN isnull(pt.version_control_ind, 'N') = 'Y' THEN xref.rss_prod_no WHEN isnull(pt.version_control_ind, 'N') = 'N' AND xref.rss_prod_no IS NULL AND xref.rss_film_id IS NULL THEN xref.rss_titleid WHEN isnull(pt.version_control_ind, 'N') = 'N' AND xref.rss_prod_no IS NOT NULL AND xref.rss_film_id IS NULL THEN substring(pc.glacct, 24, 5) -- ASR 3290687 WHEN isnull(pt.version_control_ind, 'N') = 'N' AND xref.rss_film_id IS NOT NULL THEN isnull(substring(pc.glacct, 24, 5), -- ASR 3290687 ( SELECT substring(dbo.ert_product.glacct, 24, 5) -- ASR 3290687 FROM dbo.ert_product WHERE dbo.ert_product.product_id = a.parent_product_id )) END END in (select prod_no from #prod_no) or #prod_no = 0)
Causes of duplicate results appearing in Crystal Reports
I have a crystal report using the SQL command below. I am wondering if anyone can assist me with the reason why I am getting duplicate people appearing in my report. At the moment I get multiple lines for a sales account manager (SAM), I want one line per SAM. My 1st Group Header is based on the Sector Code, 2nd group header is on command.sector, 3rd group header is on SAM SELECT DISTINCT 'Actuals' as [Trans Type], ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO as [Order Date], ER101_ACCT_ORDER_DTL.ER101_ENT_DATE_ISO as [Ent Date], year(dateadd(month,6,ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO)) as [FYR], MONTH(DATEADD(M, 6, ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO)) as [FYP], DATEADD(mm, DATEDIFF(mm, 0, ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO), 0) as [PStart], DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO) + 1, 0)) as [Pend], Revenue = Case When ER101_ACCT_ORDER_DTL.ER101_PHASE = '1' and ER101_ACCT_ORDER_DTL.ER101_COMPL_STS = 'N' then ER101_ACCT_ORDER_DTL.ER101_EXT_CHRG When ER101_ACCT_ORDER_DTL.ER101_PHASE = '5' and ER101_ACCT_ORDER_DTL.ER101_COMPL_STS = 'N' then ER101_ACCT_ORDER_DTL.ER101_EXT_CHRG Else 0 End, isnull(MM405_USER_NAME,'No SAM') AS [SAM], ER101_ACCT_ORDER_DTL.ER101_DESC as [Desc], EV200_EVENT_MASTER.EV200_EVT_ID as [EventID], isnull(SALESACCT.EV870_TYPE,'NoSec') as [Sector], isnull(CCRPF_ACCT_TYPE_DESC,'ZZ - No Sector') as [Sector Name], EV130_STATUS_MASTER.EV130_STATUS_DESC as [Event Status Desc], isnull(EV800_USER_FLD_6X,'N') as [Budgeted] FROM ER101_ACCT_ORDER_DTL WITH (NOLOCK) LEFT OUTER JOIN EV700_FUNC_MASTER WITH (NOLOCK) ON ER101_ACCT_ORDER_DTL.ER101_ORG_CODE = EV700_FUNC_MASTER.EV700_ORG_CODE AND ER101_ACCT_ORDER_DTL.ER101_EVT_ID = EV700_FUNC_MASTER.EV700_EVT_ID AND ER101_ACCT_ORDER_DTL.ER101_FUNC_ID = EV700_FUNC_MASTER.EV700_FUNC_ID LEFT OUTER JOIN EV800_SPACE_MASTER WITH (NOLOCK) ON EV700_FUNC_MASTER.EV700_SPACE = EV800_SPACE_MASTER.EV800_SPACE_CODE AND EV700_FUNC_MASTER.EV700_ORG_CODE = EV800_SPACE_MASTER.EV800_ORG_CODE LEFT OUTER JOIN EV200_EVENT_MASTER WITH (NOLOCK) ON ER101_ACCT_ORDER_DTL.ER101_ORG_CODE = EV200_EVENT_MASTER.EV200_ORG_CODE AND ER101_ACCT_ORDER_DTL.ER101_EVT_ID = EV200_EVENT_MASTER.EV200_EVT_ID LEFT OUTER JOIN EV870_ACCT_MASTER WITH (NOLOCK) ON EV200_EVENT_MASTER.EV200_ORG_CODE = EV870_ACCT_MASTER.EV870_ORG_CODE AND EV200_EVENT_MASTER.EV200_SLSPER = EV870_ACCT_MASTER.EV870_ACCT_CODE LEFT OUTER JOIN EV870_ACCT_MASTER SALESACCT WITH (NOLOCK) ON EV200_EVENT_MASTER.EV200_ORG_CODE = SALESACCT.EV870_ORG_CODE AND EV200_EVENT_MASTER.EV200_CUST_NBR = SALESACCT.EV870_ACCT_CODE LEFT OUTER JOIN CCRPF_ACCT_TYPES WITH (NOLOCK) ON SALESACCT.EV870_TYPE = CCRPF_ACCT_TYPE LEFT OUTER JOIN MM405_USER_MASTER_EXT WITH (NOLOCK) ON EV870_ACCT_MASTER.EV870_USERID = MM405_USER_MASTER_EXT.MM405_USER_ID LEFT OUTER JOIN EV130_STATUS_MASTER WITH (NOLOCK) ON EV200_EVENT_MASTER.EV200_EVT_STATUS = EV130_STATUS_MASTER.EV130_STATUS_CODE LEFT OUTER JOIN EV370_RES_MASTER WITH (NOLOCK) ON ER101_ACCT_ORDER_DTL.ER101_ORG_CODE = EV370_RES_MASTER.EV370_ORG_CODE AND ER101_ACCT_ORDER_DTL.ER101_NEW_RES_TYPE = EV370_RES_MASTER.EV370_NEW_RES_TYPE AND ER101_ACCT_ORDER_DTL.ER101_RES_CODE = EV370_RES_MASTER.EV370_RES_CODE WHERE EV200_ORG_CODE = '10' and EV200_EVT_STATUS in ('33', '34', '36', '49','50', '52') and NOT(EV200_EVT_TYPE IN ('MOS','MP','GB')) and ER101_RES_CLASS <> '8' and ER101_ORD_TYPE = 'SO' and not(EV370_RES_MASTER.EV370_MAJOR_GROUP in ('SRVCHR', 'EBOND', 'SCHARG','GIFTC')) UNION Select 'Targets' as [Trans Type], MM012_PERIOD_STDATE as [Order Date], GL010_ENT_STAMP as [Ent Date], MM012_FISCAL_YR as [FYR], MM012_FISCAL_PERIOD as [FYP], MM012_PERIOD_STDATE as [PStart], MM012_PERIOD_ENDATE as [Pend], GL010_BUD_PTD as [Revenue], 'Budget SAM' as [SAM], GL010_ACCOUNT as [Desc], null as [Event ID], GL001_SHORT_DESC as [Sector], [Sector Name] = Case When CCRPF_ACCT_TYPE_DESC is null then 'OLD - ' + GL001_DESC Else CCRPF_ACCT_TYPE_DESC End, 'Budget Event Status Desc' as [Event Status Desc], 'Targets' as [Budgeted] FROM GL010_BALANCES WITH (NOLOCK) LEFT OUTER JOIN GL001_ACCOUNT_MASTER WITH (NOLOCK) ON GL010_ORG_CODE = GL001_ORG_CODE AND GL010_ACCOUNT = GL001_ACCOUNT LEFT OUTER JOIN MM012_FISCAL_PERIODS WITH (NOLOCK) ON GL010_ORG_CODE = MM012_ORG_CODE AND GL010_FISCAL_YR = MM012_FISCAL_YR AND GL010_FISCAL_PERIOD = MM012_FISCAL_PERIOD LEFT OUTER JOIN dbo.CCRPF_ACCT_TYPES WITH (NOLOCK) ON GL001_SHORT_DESC = CCRPF_ACCT_TYPE Where GL001_ACCT_TYPE = 'SS' and LEFT(GL001_ACCOUNT_MASTER.GL001_ACCOUNT,3)='SEC'
Case Condition to join columns
Msg 156, Level 15, State 1, Line 8 Incorrect syntax near the keyword 'CASE' I am trying to join columns based on the column value. If case conditon 1 is true then i want to join ON im.ItemNumber = left(tr.itemnumber,len(tr.itemnumber)-4) if Case condition 2 is true then ON tr.ItemNumber = im.LegacyItemNumber else tr.ItemNumber = im.ItemNumber SELECT im.Category as RootCategory,GETDATE() AS LoadDate,tr.* into transactionreport_stage_testNN FROM transaction tr LEFT OUTER JOIN ALLDW_ora.dbo.Location(NOLOCK) loc ON tr.location=loc.location CASE when right(tr.itemnumber,4) = '_old' then LEFT OUTER JOIN [Inventory](NOLOCK) im ON im.ItemNumber = left(tr.itemnumber,len(tr.itemnumber)-4) when tr.itemnumber <> im.ItemNumber then LEFT OUTER JOIN [Inventory](NOLOCK) im ON tr.ItemNumber = im.LegacyItemNumber else LEFT OUTER JOIN [Inventory](NOLOCK) im ON tr.ItemNumber = im.itemnumber end AND im.Location=loc.LocationNo Schema
You should probably just use AND's and OR's LEFT OUTER JOIN [Inventory](NOLOCK) im ON im.Location=loc.LocationNo AND ( (right(tr.itemnumber,4) = '_old' AND im.ItemNumber = left(tr.itemnumber,len(tr.itemnumber)-4)) OR (tr.itemnumber <> im.ItemNumber AND tr.ItemNumber = im.LegacyItemNumber) OR (tr.ItemNumber = im.itemnumber) ) it may just be as simple as SELECT im.Category AS RootCategory, GETDATE() AS LoadDate, tr.* INTO #transactionreport_stage_testNN FROM [transaction] tr LEFT OUTER JOIN ALLDW_ora.dbo.Location (NOLOCK) loc ON tr.location = loc.location LEFT OUTER JOIN [Inventory] (NOLOCK) im ON im.Location = loc.LocationNo AND REPLACE(tr.itemnumber, '_old', '') IN (im.itemnumber, im.legacyitemnumber)