Table Spool right before insert? - sql-server
What I understand about SPOOL is to store data into a temporary table so SQL Server could use it when have CTE or other usage. Does that means SPOOL should appear in the middle of my execution plan?
With the query below(I put SELECT * here to make the query smaller), the SPOOL shows up between INSERT and CONCATENATION actions in my execution plan. Just wondering what does this SPOOL do? Cause it costs about 30% in my execution plan. Wondering if I can make the plan get rid of it.
Edit: Execution Plan uploaded
WITH FXSPOT AS (
SELECT
CURRENCY1
, FXRATE
FROM
#DBNAME#.DBO.#TBL1#
WHERE
FXSPOT.DATA_DATE = '2016-10-01'
), FTP_MTD_PRE AS (
SELECT
*
FROM #DBNAME#.DBO.#TBL2#
WHERE
RP_DATA_DT = DATEADD(D, -1, CONVERT(DATE, '2016-10-01')) AND
DAY(DATEADD(D, 1, CONVERT(DATE, RP_DATA_DT))) > 1 AND
RUN_ID = 'RC01'
), PORT AS (
SELECT
*
FROM #DBNAME#.DBO.#TBL3#
), LK_ALL AS (
SELECT
FIELDNAME
, CD
, DESCRIPTION
FROM #DBNAME#.DBO.#TBL4#
), FTP_OUT AS (
SELECT
*
FROM #DBNAME#.DBO.#TBL5#
WHERE
CURR_DT = '2016-10-01' AND
RUN_ID = 'RC01'
), DEFAULT_INDEX AS (
SELECT
IDX_FORMULA
FROM #DBNAME#.DBO.#TBL6#
WHERE
INDEX_ID = 'DEFAULT'
), STEP1 AS (
SELECT
'RC01' RUN_ID
, CONVERT(DATE, '2016-10-01') CURRENT_DT
, CONVERT(DATE, '2016-10-01') RP_DATA_DT
, CASE WHEN FTP_OUT.TXN_ID IS NULL THEN '1' ELSE '0' END ERR_FLG
, PORT.TXN_ID_R_PRODCD
, PORT.TXN_ID
, PORT.SCURITY_ID
, PORT.PRODUCT_ID
, PORT.AMRT_TYP
, PORT.CUR_PMT
, PORT.CURRENCY
, PORT.DAY_COUNT
, PORT.FIX_FLT
, PORT.FSTAMRT_DT
, PORT.FST_CPN_DT
, PORT.ISSUE_DT
, PORT.MAT_DT
, PORT.NXT_RSTDT
, PORT.ORG_PAR_BAL
, PORT.ORG_PAR_BL2
, PORT.PAY_DAY
, PORT.PMT_FLG
, PORT.PMT_FREQ
, PORT.PMT_FREQ_M
, PORT.PRV_RSTDT
, PORT.RST_FRQ
, PORT.RST_FRQ_M
, PORT.SNK_DAY
, PORT.SNK_PMT
, PORT.SNK_RT
, PORT.SNK_FRQ
, PORT.SNK_FRQ_M
, PORT.SNK_P_FLG
, PORT.TENOR
, PORT.TENOR_M
, PORT.TP_ADJ1_ID
, PORT.TP_ADJ2_ID
, PORT.TP_ADJ3_ID
, PORT.TP_ADJ4_ID
, PORT.TP_ADJ5_ID
, PORT.TP_ID
, PORT.TP_INDEX_ID
, PORT.TRANSPRICE
, PORT.ASST_LIAB
, PORT.PAR_BAL
, PORT.CUR_BK_BAL
, PORT.CUR_BK_BAL * FXSPOT.FXRATE CUR_BK_BAL_DOM
, PORT.T_CUR_BK_BAL
, PORT.T_CUR_BK_BAL * FXSPOT.FXRATE T_CUR_BK_BAL_DOM
, PORT.CUR_GRS_RT
, PORT.T_UNEARN
, PORT.T_UNEARN * FXSPOT.FXRATE T_UNEARN_DOM
, PORT.T_CURBAL
, PORT.T_CURBAL * FXSPOT.FXRATE T_CURBAL_DOM
, PORT.T_POINTAMT
, PORT.T_POINTAMT * FXSPOT.FXRATE T_POINTAMT_DOM
, PORT.T_RESTBALC
, PORT.T_RESTBALC * FXSPOT.FXRATE T_RESTBALC_DOM
, PORT.G_BANKNO
, PORT.G_CSTCTR
, PORT.G_GLACCTNO
, PORT.G_GLACCTYP
, PORT.G_GLACCTINT
, PORT.T_RTTYP
, PORT.T_PMTTYP
, PORT.T_NIDPMMD
, PORT.T_CAHSSS
, PORT.R_LOBCD
, PORT.R_BRNCHCD
, PORT.R_PRODCD
, PORT.R_TYPCD
, PORT.R_HPTYPCD
, PORT.R_NAME R_NAME
, PORT.R_CUSTCODE R_CUSTCODE
, PORT.R_PRODTYP R_PRODTYP
, PORT.R_INTPLAN R_INTPLAN
, PORT.R_DEPGRP_NAME R_DEPGRP_NAME
, PORT.R_DEPID R_DEPID
, PORT.R_MISC_IND R_MISC_IND
, PORT.R_SRCTBL R_SRCTBL
, PORT.R_RESTIND R_RESTIND
, PORT.R_SECNO R_SECNO
, PORT.R_PAIDIND R_PAIDIND
, COALESCE(FTP_OUT.TP_ORIGNAL, DEFAULT_INDEX.IDX_FORMULA) TP_ORIGNAL
, FTP_OUT.TP_ADJ1 TP_ADJ1
, FTP_OUT.TP_ADJ2 TP_ADJ2
, FTP_OUT.TP_ADJ3 TP_ADJ3
, FTP_OUT.TP_ADJ4 TP_ADJ4
, FTP_OUT.TP_ADJ5 TP_ADJ5
, COALESCE(FTP_OUT.TP_FINAL, DEFAULT_INDEX.IDX_FORMULA) TP_FINAL
, FTP_OUT.DURATION DURATION
, FTP_OUT.WAL WAL
, FTP_OUT.USER_INPUT USER_INPUT
, COALESCE(FTP_OUT.TP_MODEL, 'D') TP_MODEL
, FTP_OUT.TP_CLASS TP_CLASS
, FTP_OUT.YC_DT YC_DT
, FTP_OUT.START_DT START_DT
, FTP_OUT.END_DT END_DT
, (PORT.CUR_GRS_RT - COALESCE(FTP_OUT.TP_FINAL, DEFAULT_INDEX.IDX_FORMULA)) * PORT.ASST_LIAB TR_FINAL_SPREAD
, (PORT.CUR_GRS_RT - COALESCE(FTP_OUT.TP_ORIGNAL, DEFAULT_INDEX.IDX_FORMULA)) * PORT.ASST_LIAB TR_SPREAD
, PORT.T_CUR_BK_BAL * FXSPOT.FXRATE * PORT.CUR_GRS_RT / 100.0 * PORT.DAY_COUNT_VALUE * PORT.ASST_LIAB TR_IE_INT
, PORT.T_CUR_BK_BAL * FXSPOT.FXRATE * FTP_OUT.TP_ADJ1 / 100.0 * PORT.DAY_COUNT_VALUE * PORT.ASST_LIAB * -1 TR_IE_TP_ADJ1
, PORT.T_CUR_BK_BAL * FXSPOT.FXRATE * FTP_OUT.TP_ADJ2 / 100.0 * PORT.DAY_COUNT_VALUE * PORT.ASST_LIAB * -1 TR_IE_TP_ADJ2
, PORT.T_CUR_BK_BAL * FXSPOT.FXRATE * FTP_OUT.TP_ADJ3 / 100.0 * PORT.DAY_COUNT_VALUE * PORT.ASST_LIAB * -1 TR_IE_TP_ADJ3
, PORT.T_CUR_BK_BAL * FXSPOT.FXRATE * FTP_OUT.TP_ADJ4 / 100.0 * PORT.DAY_COUNT_VALUE * PORT.ASST_LIAB * -1 TR_IE_TP_ADJ4
, PORT.T_CUR_BK_BAL * FXSPOT.FXRATE * FTP_OUT.TP_ADJ5 / 100.0 * PORT.DAY_COUNT_VALUE * PORT.ASST_LIAB * -1 TR_IE_TP_ADJ5
, PORT.T_CUR_BK_BAL * FXSPOT.FXRATE * COALESCE(FTP_OUT.TP_ORIGNAL, DEFAULT_INDEX.IDX_FORMULA) / 100.0 * PORT.DAY_COUNT_VALUE * PORT.ASST_LIAB * -1 TR_IE_TP_ORG
, PORT.T_CUR_BK_BAL * FXSPOT.FXRATE * COALESCE(FTP_OUT.TP_FINAL, DEFAULT_INDEX.IDX_FORMULA) / 100.0 * PORT.DAY_COUNT_VALUE * PORT.ASST_LIAB * -1 TR_IE_TRNS
, COALESCE(FTP_OUT.TP_FINAL, DEFAULT_INDEX.IDX_FORMULA) TR_TP_FINAL
, FXSPOT.FXRATE
, DAY(CONVERT(DATE, '2016-10-01')) TR_DAYS_MTD_TODAY
FROM PORT
LEFT JOIN FTP_OUT ON PORT.TXN_ID = FTP_OUT.TXN_ID
LEFT JOIN FXSPOT ON PORT.CURRENCY = FXSPOT.CURRENCY1
LEFT JOIN DEFAULT_INDEX ON 1 = 1
), STEP2 AS (
SELECT
STEP1.*
, FTP_MTD_PRE.TXN_STATUS_FLG TXN_STATUS_FLG_PRE
, CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN '0' WHEN FTP_MTD_PRE.TXN_ID_R_PRODCD IS NOT NULL THEN '1' WHEN FTP_MTD_PRE.TXN_ID_R_PRODCD IS NULL THEN '2' END TXN_STATUS_FLG
, STEP1.T_CUR_BK_BAL_DOM + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_BAL_MTD, 0) END TR_BAL_MTD
, STEP1.T_CUR_BK_BAL_DOM + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_BAL_MTD, 0) END / DAY(CONVERT(DATE, RP_DATA_DT)) TR_AVGBAL_MTD
, STEP1.T_CURBAL_DOM + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_BAL_MTD, 0) END TR_CURBAL_MTD
, STEP1.T_CURBAL_DOM + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_BAL_MTD, 0) END / DAY(CONVERT(DATE, RP_DATA_DT)) TR_AVGCURBAL_MTD
, STEP1.TR_IE_TRNS * -1 TR_FC_TRNS
, STEP1.TR_IE_INT + STEP1.TR_IE_TRNS TR_MARGIN
, STEP1.T_POINTAMT_DOM + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_POINTAMT_MTD, 0) END TR_POINTAMT_MTD
, STEP1.T_POINTAMT_DOM + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_POINTAMT_MTD, 0) END / DAY(CONVERT(DATE, RP_DATA_DT)) TR_AVGPOINTAMT_MTD
, STEP1.T_RESTBALC_DOM + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_RESTBALC_MTD, 0) END TR_RESTBALC_MTD
, STEP1.T_RESTBALC_DOM + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_RESTBALC_MTD, 0) END / DAY(CONVERT(DATE, RP_DATA_DT)) TR_AVGRESTBALC_MTD
, STEP1.T_UNEARN_DOM + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_UNEARN_MTD, 0) END TR_UNEARN_MTD
, STEP1.T_UNEARN_DOM + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_UNEARN_MTD, 0) END / DAY(CONVERT(DATE, RP_DATA_DT)) TR_AVGUNEARN_MTD
, STEP1.TR_IE_TRNS * -1 + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_FCTRNS_MTD, 0) END TR_FCTRNS_MTD
, STEP1.TR_IE_TP_ADJ1 + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_IE_TP_ADJ1_MTD, 0) END TR_IE_TP_ADJ1_MTD
, STEP1.TR_IE_TP_ADJ2 + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_IE_TP_ADJ2_MTD, 0) END TR_IE_TP_ADJ2_MTD
, STEP1.TR_IE_TP_ADJ3 + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_IE_TP_ADJ3_MTD, 0) END TR_IE_TP_ADJ3_MTD
, STEP1.TR_IE_TP_ADJ4 + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_IE_TP_ADJ4_MTD, 0) END TR_IE_TP_ADJ4_MTD
, STEP1.TR_IE_TP_ADJ5 + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_IE_TP_ADJ5_MTD, 0) END TR_IE_TP_ADJ5_MTD
, STEP1.TR_IE_TP_ORG + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_IE_TP_ORG_MTD, 0) END TR_IE_TP_ORG_MTD
, STEP1.TR_IE_INT + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_IEINT_MTD, 0) END TR_IEINT_MTD
, STEP1.TR_IE_TRNS + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_IETRNS_MTD, 0) END TR_IETRNS_MTD
, STEP1.TR_IE_INT + STEP1.TR_IE_TRNS + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.TR_MARGIN_MTD, 0) END TR_MARGIN_MTD
, STEP1.CUR_GRS_RT * STEP1.T_CUR_BK_BAL_DOM * STEP1.ASST_LIAB W_CUR_GRS_RT
, (STEP1.CUR_GRS_RT * STEP1.T_CUR_BK_BAL_DOM * STEP1.ASST_LIAB) + CASE WHEN TR_DAYS_MTD_TODAY = 1 THEN 0 ELSE ISNULL(FTP_MTD_PRE.W_CUR_GRS_RT_MTD, 0) END W_CUR_GRS_RT_MTD
, STEP1.TP_ADJ1 * STEP1.T_CUR_BK_BAL_DOM * STEP1.ASST_LIAB * -1 W_TP_ADJ1
, STEP1.TP_ADJ2 * STEP1.T_CUR_BK_BAL_DOM * STEP1.ASST_LIAB * -1 W_TP_ADJ2
, STEP1.TP_ADJ3 * STEP1.T_CUR_BK_BAL_DOM * STEP1.ASST_LIAB * -1 W_TP_ADJ3
, STEP1.TP_ADJ4 * STEP1.T_CUR_BK_BAL_DOM * STEP1.ASST_LIAB * -1 W_TP_ADJ4
, STEP1.TP_ADJ5 * STEP1.T_CUR_BK_BAL_DOM * STEP1.ASST_LIAB * -1 W_TP_ADJ5
, STEP1.TR_FINAL_SPREAD * STEP1.T_CUR_BK_BAL_DOM * STEP1.ASST_LIAB * -1 W_TR_FINAL_SPREAD
, STEP1.TP_FINAL * STEP1.T_CUR_BK_BAL_DOM * STEP1.ASST_LIAB * -1 W_TR_TP_FINAL
, STEP1.TP_ORIGNAL * STE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_ADJ5
, AVG_TR_FINAL_SPREAD
, AVG_TR_TP_FINAL
, G_BANKNO
, G_CSTCTR
, G_GLACCTNO
, G_GLACCTYP
, G_GLACCTINT
, T_RTTYP
, T_PMTTYP
, T_NIDPMMD
, T_CAHSSS
, R_LOBCD
, R_BRNCHCD
, R_PRODCD
, R_TYPCD
, R_HPTYPCD
, DSC_G_BANKNO
, DSC_G_CSTCTR
, DSC_G_GLACCTNO
, DSC_G_GLACCTYP
, DSC_G_GLACCTINT
, DSC_T_RTTYP
, DSC_T_PMTTYP
, DSC_T_NIDPMMD
, DSC_T_CAHSSS
, DSC_R_LOBCD
, DSC_R_BRNCHCD
, DSC_R_PRODCD
, DSC_R_TYPCD
, DSC_R_HPTYPCD
, R_NAME
, R_CUSTCODE
, R_PRODTYP
, R_INTPLAN
, R_DEPGRP_NAME
, R_DEPID
, R_MISC_IND
, R_SRCTBL
, R_RESTIND
, R_SECNO
, R_PAIDIND
, T_UNEARN
, T_UNEARN_DOM
, TR_UNEARN_MTD
, TR_AVGUNEARN_MTD
, T_CURBAL
, T_CURBAL_DOM
, TR_CURBAL_MTD
, TR_AVGCURBAL_MTD
, T_POINTAMT
, T_POINTAMT_DOM
, TR_POINTAMT_MTD
, TR_AVGPOINTAMT_MTD
, T_RESTBALC
, T_RESTBALC_DOM
, TR_RESTBALC_MTD
, TR_AVGRESTBALC_MTD
, CURR_DT
, R_PROCESS_DT
)
SELECT
STEP2.RP_DATA_DT
, STEP2.TXN_ID_R_PRODCD
, STEP2.TXN_STATUS_FLG
, STEP2.TXN_STATUS_FLG_PRE
, STEP2.ERR_FLG
, STEP2.TXN_ID
, STEP2.SCURITY_ID
, STEP2.PRODUCT_ID
, STEP2.AMRT_TYP
, STEP2.ASST_LIAB
, STEP2.CUR_BK_BAL
, STEP2.T_CUR_BK_BAL
, STEP2.CUR_GRS_RT
, STEP2.CUR_PMT
, STEP2.CURRENCY
, STEP2.DAY_COUNT
, STEP2.FIX_FLT
, STEP2.FSTAMRT_DT
, STEP2.FST_CPN_DT
, STEP2.ISSUE_DT
, STEP2.MAT_DT
, STEP2.NXT_RSTDT
, STEP2.ORG_PAR_BAL
, STEP2.ORG_PAR_BL2
, STEP2.PAY_DAY
, STEP2.PMT_FLG
, STEP2.PMT_FREQ
, STEP2.PMT_FREQ_M
, STEP2.PRV_RSTDT
, STEP2.RST_FRQ
, STEP2.RST_FRQ_M
, STEP2.SNK_DAY
, STEP2.SNK_FRQ
, STEP2.SNK_FRQ_M
, STEP2.SNK_P_FLG
, STEP2.SNK_PMT
, STEP2.SNK_RT
, STEP2.TENOR
, STEP2.TENOR_M
, STEP2.TP_ADJ1_ID
, STEP2.TP_ADJ2_ID
, STEP2.TP_ADJ3_ID
, STEP2.TP_ADJ4_ID
, STEP2.TP_ADJ5_ID
, STEP2.TP_ID
, STEP2.TP_INDEX_ID
, STEP2.TRANSPRICE
, STEP2.RUN_ID
, STEP2.CURRENT_DT
, STEP2.TP_ORIGNAL
, STEP2.TP_ADJ1
, STEP2.TP_ADJ2
, STEP2.TP_ADJ3
, STEP2.TP_ADJ4
, STEP2.TP_ADJ5
, STEP2.TP_FINAL
, STEP2.DURATION
, STEP2.WAL
, CASE WHEN STEP2.USER_INPUT = '' THEN NULL ELSE STEP2.USER_INPUT END USER_INPUT
, CASE WHEN STEP2.TP_MODEL = '' THEN NULL ELSE STEP2.TP_MODEL END TP_MODEL
, STEP2.TP_CLASS
, STEP2.YC_DT
, STEP2.START_DT
, STEP2.END_DT
, STEP2.FXRATE
, STEP2.CUR_BK_BAL_DOM
, STEP2.T_CUR_BK_BAL_DOM
, STEP2.TR_SPREAD
, STEP2.TR_FINAL_SPREAD
, STEP2.TR_TP_FINAL
, STEP2.TR_IE_INT
, STEP2.TR_IE_TP_ORG
, STEP2.TR_IE_TP_ADJ1
, STEP2.TR_IE_TP_ADJ2
, STEP2.TR_IE_TP_ADJ3
, STEP2.TR_IE_TP_ADJ4
, STEP2.TR_IE_TP_ADJ5
, STEP2.TR_IE_TRNS
, STEP2.TR_MARGIN
, STEP2.TR_FC_TRNS
, STEP2.TR_DAYS_MTD_TODAY
, STEP2.TR_BAL_MTD
, STEP2.TR_AVGBAL_MTD
, STEP2.TR_IEINT_MTD
, STEP2.TR_IE_TP_ORG_MTD
, STEP2.TR_IE_TP_ADJ1_MTD
, STEP2.TR_IE_TP_ADJ2_MTD
, STEP2.TR_IE_TP_ADJ3_MTD
, STEP2.TR_IE_TP_ADJ4_MTD
, STEP2.TR_IE_TP_ADJ5_MTD
, STEP2.TR_IETRNS_MTD
, STEP2.TR_MARGIN_MTD
, STEP2.TR_FCTRNS_MTD
, STEP2.W_CUR_GRS_RT
, STEP2.W_TR_TP_ORIGNAL
, STEP2.W_TP_ADJ1
, STEP2.W_TP_ADJ2
, STEP2.W_TP_ADJ3
, STEP2.W_TP_ADJ4
, STEP2.W_TP_ADJ5
, STEP2.W_TR_FINAL_SPREAD
, STEP2.W_TR_TP_FINAL
, STEP2.W_CUR_GRS_RT_MTD
, STEP2.W_TR_TP_ORIGNAL + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TR_TP_ORIGNAL_MTD END W_TR_TP_ORIGNAL_MTD
, STEP2.W_TP_ADJ1 + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TP_ADJ1_MTD END W_TP_ADJ1_MTD
, STEP2.W_TP_ADJ2 + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TP_ADJ2_MTD END W_TP_ADJ2_MTD
, STEP2.W_TP_ADJ3 + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TP_ADJ3_MTD END W_TP_ADJ3_MTD
, STEP2.W_TP_ADJ4 + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TP_ADJ4_MTD END W_TP_ADJ4_MTD
, STEP2.W_TP_ADJ5 + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TP_ADJ5_MTD END W_TP_ADJ5_MTD
, STEP2.W_TR_FINAL_SPREAD + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TR_FINAL_SPREAD_MTD END W_TR_FINAL_SPREAD_MTD
, STEP2.W_TR_TP_FINAL + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TR_TP_FINAL_MTD END W_TR_TP_FINAL_MTD
, CASE WHEN STEP2.TR_BAL_MTD = 0 THEN 0 ELSE STEP2.W_CUR_GRS_RT_MTD / STEP2.TR_BAL_MTD END AVG_CUR_GRS_RT
, CASE WHEN STEP2.TR_BAL_MTD = 0 THEN 0 ELSE (STEP2.W_TR_TP_ORIGNAL + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TR_TP_ORIGNAL_MTD END) / STEP2.TR_BAL_MTD END AVG_TR_TP_ORIGNAL
, CASE WHEN STEP2.TR_BAL_MTD = 0 THEN 0 ELSE (STEP2.W_TP_ADJ1 + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TP_ADJ1_MTD END) / STEP2.TR_BAL_MTD END AVG_TP_ADJ1
, CASE WHEN STEP2.TR_BAL_MTD = 0 THEN 0 ELSE (STEP2.W_TP_ADJ2 + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TP_ADJ2_MTD END) / STEP2.TR_BAL_MTD END AVG_TP_ADJ2
, CASE WHEN STEP2.TR_BAL_MTD = 0 THEN 0 ELSE (STEP2.W_TP_ADJ3 + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TP_ADJ3_MTD END) / STEP2.TR_BAL_MTD END AVG_TP_ADJ3
, CASE WHEN STEP2.TR_BAL_MTD = 0 THEN 0 ELSE (STEP2.W_TP_ADJ4 + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TP_ADJ4_MTD END) / STEP2.TR_BAL_MTD END AVG_TP_ADJ4
, CASE WHEN STEP2.TR_BAL_MTD = 0 THEN 0 ELSE (STEP2.W_TP_ADJ5 + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TP_ADJ5_MTD END) / STEP2.TR_BAL_MTD END AVG_TP_ADJ5
, CASE WHEN STEP2.TR_BAL_MTD = 0 THEN 0 ELSE (STEP2.W_TR_FINAL_SPREAD + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TR_FINAL_SPREAD_MTD END) / STEP2.TR_BAL_MTD END AVG_TR_FINAL_SPREAD
, CASE WHEN STEP2.TR_BAL_MTD = 0 THEN 0 ELSE (STEP2.W_TR_TP_FINAL + CASE WHEN STEP2.TXN_STATUS_FLG = 0 THEN 0 ELSE STEP2.P_W_TR_TP_FINAL_MTD END) / STEP2.TR_BAL_MTD END AVG_TR_TP_FINAL
, STEP2.G_BANKNO
, STEP2.G_CSTCTR
, STEP2.G_GLACCTNO
, STEP2.G_GLACCTYP
, STEP2.G_GLACCTINT
, STEP2.T_RTTYP
, STEP2.T_PMTTYP
, STEP2.T_NIDPMMD
, STEP2.T_CAHSSS
, STEP2.R_LOBCD
, STEP2.R_BRNCHCD
, STEP2.R_PRODCD
, STEP2.R_TYPCD
, STEP2.R_HPTYPCD
, D1.DESCRIPTION DSC_G_BANKNO
, D2.DESCRIPTION DSC_G_CSTCTR
, D3.DESCRIPTION DSC_G_GLACCTNO
, D4.DESCRIPTION DSC_G_GLACCTYP
, D5.DESCRIPTION DSC_G_GLACCTINT
, D6.DESCRIPTION DSC_T_RTTYP
, D7.DESCRIPTION DSC_T_PMTTYP
, D8.DESCRIPTION DSC_T_NIDPMMD
, D9.DESCRIPTION DSC_T_CAHSSS
, D10.DESCRIPTION DSC_R_LOBCD
, D11.DESCRIPTION DSC_R_BRNCHCD
, D12.DESCRIPTION DSC_R_PRODCD
, D13.DESCRIPTION DSC_R_TYPCD
, D14.DESCRIPTION DSC_R_HPTYPCD
, STEP2.R_NAME
, STEP2.R_CUSTCODE
, STEP2.R_PRODTYP
, STEP2.R_INTPLAN
, STEP2.R_DEPGRP_NAME
, STEP2.R_DEPID
, STEP2.R_MISC_IND
, STEP2.R_SRCTBL
, STEP2.R_RESTIND
, STEP2.R_SECNO
, STEP2.R_PAIDIND
, STEP2.T_UNEARN
, STEP2.T_UNEARN_DOM
, STEP2.TR_UNEARN_MTD
, STEP2.TR_AVGUNEARN_MTD
, STEP2.T_CURBAL
, STEP2.T_CURBAL_DOM
, STEP2.TR_CURBAL_MTD
, STEP2.TR_AVGCURBAL_MTD
, STEP2.T_POINTAMT
, STEP2.T_POINTAMT_DOM
, STEP2.TR_POINTAMT_MTD
, STEP2.TR_AVGPOINTAMT_MTD
, STEP2.T_RESTBALC
, STEP2.T_RESTBALC_DOM
, STEP2.TR_RESTBALC_MTD
, STEP2.TR_AVGRESTBALC_MTD
, RP_DATA_DT CURR_DT
, CONVERT(DATE, GETDATE()) R_PROCESS_DT
FROM STEP2
LEFT JOIN LK_ALL D1 ON D1.FIELDNAME = 'G_BANKNO' AND STEP2.G_BANKNO = D1.CD
LEFT JOIN LK_ALL D2 ON D2.FIELDNAME = 'G_CSTCTR' AND STEP2.G_CSTCTR = D2.CD
LEFT JOIN LK_ALL D3 ON D3.FIELDNAME = 'G_GLACCTNO' AND STEP2.G_GLACCTNO = D3.CD
LEFT JOIN LK_ALL D4 ON D4.FIELDNAME = 'G_GLACCTYP' AND STEP2.G_GLACCTYP = D4.CD
LEFT JOIN LK_ALL D5 ON D5.FIELDNAME = 'G_GLACCTINT' AND STEP2.G_GLACCTINT = D5.CD
LEFT JOIN LK_ALL D6 ON D6.FIELDNAME = 'T_RTTYP' AND STEP2.T_RTTYP = D6.CD
LEFT JOIN LK_ALL D7 ON D7.FIELDNAME = 'T_PMTTYP' AND STEP2.T_PMTTYP = D7.CD
LEFT JOIN LK_ALL D8 ON D8.FIELDNAME = 'T_NIDPMMD' AND STEP2.T_NIDPMMD = D8.CD
LEFT JOIN LK_ALL D9 ON D9.FIELDNAME = 'T_CAHSSS' AND STEP2.T_CAHSSS = D9.CD
LEFT JOIN LK_ALL D10 ON D10.FIELDNAME = 'R_LOBCD' AND STEP2.R_LOBCD = D10.CD
LEFT JOIN LK_ALL D11 ON D11.FIELDNAME = 'R_BRNCHCD' AND STEP2.R_BRNCHCD = D11.CD
LEFT JOIN LK_ALL D12 ON D12.FIELDNAME = 'R_PRODCD' AND STEP2.R_PRODCD = D12.CD
LEFT JOIN LK_ALL D13 ON D13.FIELDNAME = 'R_TYPCD' AND STEP2.R_TYPCD = D13.CD
LEFT JOIN LK_ALL D14 ON D14.FIELDNAME = 'R_HPTYPCD' AND STEP2.R_HPTYPCD = D14.CD
UNION ALL
SELECT
'2016-10-01' RP_DATA_DT
, FTP_MTD_PRE.TXN_ID_R_PRODCD
, '3' TXN_STATUS_FLG
, FTP_MTD_PRE.TXN_STATUS_FLG
, FTP_MTD_PRE.ERR_FLG
, FTP_MTD_PRE.TXN_ID
, FTP_MTD_PRE.SCURITY_ID
, FTP_MTD_PRE.PRODUCT_ID
, FTP_MTD_PRE.AMRT_TYP
, FTP_MTD_PRE.ASST_LIAB
, 0 CUR_BK_BAL
, 0 T_CUR_BK_BAL
, FTP_MTD_PRE.CUR_GRS_RT
, FTP_MTD_PRE.CUR_PMT
, FTP_MTD_PRE.CURRENCY
, FTP_MTD_PRE.DAY_COUNT
, FTP_MTD_PRE.FIX_FLT
, FTP_MTD_PRE.FSTAMRT_DT
, FTP_MTD_PRE.FST_CPN_DT
, FTP_MTD_PRE.ISSUE_DT
, FTP_MTD_PRE.MAT_DT
, FTP_MTD_PRE.NXT_RSTDT
, FTP_MTD_PRE.ORG_PAR_BAL
, FTP_MTD_PRE.ORG_PAR_BL2
, FTP_MTD_PRE.PAY_DAY
, FTP_MTD_PRE.PMT_FLG
, FTP_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P
, FTP_MTD_PRE.R_INTPLAN
, FTP_MTD_PRE.R_DEPGRP_NAME
, FTP_MTD_PRE.R_DEPID
, FTP_MTD_PRE.R_MISC_IND
, FTP_MTD_PRE.R_SRCTBL
, FTP_MTD_PRE.R_RESTIND
, FTP_MTD_PRE.R_SECNO
, FTP_MTD_PRE.R_PAIDIND
, 0 T_UNEARN
, 0 T_UNEARN_DOM
, FTP_MTD_PRE.TR_UNEARN_MTD
, FTP_MTD_PRE.TR_AVGUNEARN_MTD
, 0 T_CURBAL
, 0 T_CURBAL_DOM
, FTP_MTD_PRE.TR_CURBAL_MTD
, FTP_MTD_PRE.TR_AVGCURBAL_MTD
, 0 T_POINTAMT
, 0 T_POINTAMT_DOM
, FTP_MTD_PRE.TR_POINTAMT_MTD
, FTP_MTD_PRE.TR_AVGPOINTAMT_MTD
, 0 T_RESTBALC
, 0 T_RESTBALC_DOM
, FTP_MTD_PRE.TR_RESTBALC_MTD
, FTP_MTD_PRE.TR_AVGRESTBALC_MTD
, '2016-10-01' CURR_DT
, CONVERT(DATE, GETDATE()) R_PROCESS_DT
FROM FTP_MTD_PRE
WHERE NOT EXISTS (SELECT 1 FROM PORT WHERE FTP_MTD_PRE.TXN_ID_R_PRODCD = PORT.TXN_ID_R_PRODCD)
Soool is copy of data in temp db .
Query have cte used to create new cte so execution logic is getting complicated to solve the problem sql server need to create interim result set the same is visible in query plan as spool.
https://technet.microsoft.com/en-us/library/ms181032(v=sql.105).aspx
IMHO , there is no point in CTE where there is no filter or calculation
for example LK_ALL,you can directly use #DBNAME#.DBO.#TBL4# where req. it may or may not improve performance,but at least it reduce number of lines.
It is really tough to debug, whole query.
but see here instead of so many left join(BTW,if you can use inner join)
you can rewrite your query like this,
FROM STEP2
Inner/LEFT JOIN LK_ALL D1 ON (STEP2.G_BANKNO = D1.CD AND D1.FIELDNAME = 'G_BANKNO')
or (D2.FIELDNAME = 'G_CSTCTR' AND STEP2.G_CSTCTR = D2.CD )
or (D3.FIELDNAME = 'G_GLACCTNO' AND STEP2.G_GLACCTNO = D3.CD )
or (D4.FIELDNAME = 'G_GLACCTYP' AND STEP2.G_GLACCTYP = D4.CD )
or (D5.FIELDNAME = 'G_GLACCTINT' AND STEP2.G_GLACCTINT = D5.CD )
or (D6.FIELDNAME = 'T_RTTYP' AND STEP2.T_RTTYP = D6.CD )
or (D7.FIELDNAME = 'T_PMTTYP' AND STEP2.T_PMTTYP = D7.CD )
or (D8.FIELDNAME = 'T_NIDPMMD' AND STEP2.T_NIDPMMD = D8.CD )
or (D9.FIELDNAME = 'T_CAHSSS' AND STEP2.T_CAHSSS = D9.CD )
or ( D10.FIELDNAME = 'R_LOBCD' AND STEP2.R_LOBCD = D10.CD )
or ( D11.FIELDNAME = 'R_BRNCHCD' AND STEP2.R_BRNCHCD = D11.CD )
or ( D12.FIELDNAME = 'R_PRODCD' AND STEP2.R_PRODCD = D12.CD )
or ( D13.FIELDNAME = 'R_TYPCD' AND STEP2.R_TYPCD = D13.CD )
or ( D14.FIELDNAME = 'R_HPTYPCD' AND STEP2.R_HPTYPCD = D14.CD )
I hope those hard coded parameter are dynamic in real life
Also you can do like,
declare #From date= dateadd(day,-1,'2016-10-01')
declare #To int= DAY(DATEADD(D, 1, CONVERT(DATE, RP_DATA_DT)))
for millions of records temp table is better than CTE.
Also you can comment part of query to see the performance.
Related
Slow Query only for specific values in a Where IN Clause
I wonder if anyone has come across this behaviour before in the following query if I filter on "i.InspectionTypeID IN (x)" if x results in a large number (like about 2/3 of the entire result set of about 600 000) It takes a significantly longer amount of time to return even no results (15-30 seconds vs instantly otherwise). I can include the query and the execution plans if that helps. I can see a few differences between the queries but no idea how to refactor to improve it. We have indexes around the place that should (and have) improved performance in other places but I seem to be stuck on this one. Indexes don't appear to affect performance. At least what I tried. Slow Query Plan: https://www.brentozar.com/pastetheplan/?id=SyBDgxSeO EDIT: New Plan and Updated the Query as per advice from #charlieface Here's the query: declare #currentUserID int = 1018 declare #currentCompanyID int = 43 declare #status int = 0 declare #pageSize int = 2147483647 declare #pageNumber int = 1 DECLARE #notPreferredAssetIDsTable TABLE (id INT) INSERT INTO #notPreferredAssetIDsTable SELECT RecordID FROM SettingList sl WHERE SettingID = (SELECT TOP 1 ID FROM Setting WHERE SystemUserID = #currentUserID AND Name = 'Preferred Assets' AND Status = #status) AND Value = '0' AND Status = #status SELECT i.ID As ID, a.AssetTypeID, at.Name as AssetTypeName, a.Code as AssetCode, a.Name as AssetName, CASE WHEN sections.SectionCount = 1 THEN cr.SectionName ELSE '' END as SectionName, c.Name as ContractName, reg.Name as RegionName, i.Carriageway, startStatus.StatusDate as StartDate, startPoint.Name as StartPointName, i.ChainageFrom, CASE WHEN i.Carriageway IS NULL THEN NULL ELSE i.ChainageFrom - (CASE WHEN i.Carriageway = 0 THEN startPoint.Forward ELSE startPoint.Reverse END) END StartDistancePast, completedStatus.StatusDate As EndDate, endPoint.Name as EndPointName, i.ChainageTo, CASE WHEN i.Carriageway IS NULL THEN NULL ELSE i.ChainageTo - (CASE WHEN i.Carriageway = 0 THEN endPoint.Forward ELSE endPoint.Reverse END) END EndDistancePast, CASE WHEN i.Carriageway IS NULL THEN '' ELSE (CASE WHEN (ISNULL(OtherDirectionInspection.ID, 0) > 0) THEN 'Forward/Reverse' WHEN i.Carriageway = 0 THEN 'Forward' ELSE 'Reverse' END) END as CarriagewayName, dbo.GetInspectionTypeNamesForInspectionID(i.ID) AS InspectionTypeName, JobCount.Total As JobCount, i.CreatedDate, CreatedUserName.DisplayName as CreatedUserName, AssignedUserName.DisplayName as AssignedUserName, i.Comments, i.EntireRoad, i.PlannedDate, latestStatus.InspectionStatus AS LatestInspectionStatus, completedStatus.StatusDate AS CompletedDate, it.CompanyID as InspectionTypeCompanyID, i.ContractID, i.InspectionTypeID, sections.SectionCount, it.Category as InspectionTypeCategory, latestStatus.ModifiedDeviceID as DeviceID, latestStatus.ModifiedUserID as LatestStatusModifiedUserID, i.JobID, i.CapitalWorkID, cw.Name as CapitalWorkName, AssignedUser.Name as AssignedUserEmail, InspectionRouteName.Name as InspectionRouteName, i.InspectionGroupID, il.Location AS InspectionLocation, i.OtherDirectionInspectionID , CustomText1 , CustomBit1 , CustomDate1 , CustomNumber1 , CustomReferenceItemID1 , CustomText2 , CustomBit2 , CustomDate2 , CustomNumber2 , CustomReferenceItemID2 , CustomText3 , CustomBit3 , CustomDate3 , CustomNumber3 , CustomReferenceItemID3 , CustomText4 , CustomBit4 , CustomDate4 , CustomNumber4 , CustomReferenceItemID4 , CustomText5 , CustomBit5 , CustomDate5 , CustomNumber5 , CustomReferenceItemID5 , CustomText6 , CustomBit6 , CustomDate6 , CustomNumber6 , CustomReferenceItemID6 , CustomText7 , CustomBit7 , CustomDate7 , CustomNumber7 , CustomReferenceItemID7 , CustomText8 , CustomBit8 , CustomDate8 , CustomNumber8 , CustomReferenceItemID8 , CustomText9 , CustomBit9 , CustomDate9 , CustomNumber9 , CustomReferenceItemID9 , CustomText10 , CustomBit10 , CustomDate10 , CustomNumber10 , CustomReferenceItemID10 , CustomText11 , CustomBit11 , CustomDate11 , CustomNumber11 , CustomReferenceItemID11 , CustomText12 , CustomBit12 , CustomDate12 , CustomNumber12 , CustomReferenceItemID12 , CustomText13 , CustomBit13 , CustomDate13 , CustomNumber13 , CustomReferenceItemID13 , CustomText14 , CustomBit14 , CustomDate14 , CustomNumber14 , CustomReferenceItemID14 , CustomText15 , CustomBit15 , CustomDate15 , CustomNumber15 , CustomReferenceItemID15 , CustomText16 , CustomBit16 , CustomDate16 , CustomNumber16 , CustomReferenceItemID16 , CustomText17 , CustomBit17 , CustomDate17 , CustomNumber17 , CustomReferenceItemID17 , CustomText18 , CustomBit18 , CustomDate18 , CustomNumber18 , CustomReferenceItemID18 , CustomText19 , CustomBit19 , CustomDate19 , CustomNumber19 , CustomReferenceItemID19 , CustomText20 , CustomBit20 , CustomDate20 , CustomNumber20 , CustomReferenceItemID20 , CustomText21 , CustomBit21 , CustomDate21 , CustomNumber21 , CustomReferenceItemID21 , CustomText22 , CustomBit22 , CustomDate22 , CustomNumber22 , CustomReferenceItemID22 , CustomText23 , CustomBit23 , CustomDate23 , CustomNumber23 , CustomReferenceItemID23 , CustomText24 , CustomBit24 , CustomDate24 , CustomNumber24 , CustomReferenceItemID24 , CustomText25 , CustomBit25 , CustomDate25 , CustomNumber25 , CustomReferenceItemID25 , CustomText26 , CustomBit26 , CustomDate26 , CustomNumber26 , CustomReferenceItemID26 , CustomText27 , CustomBit27 , CustomDate27 , CustomNumber27 , CustomReferenceItemID27 , CustomText28 , CustomBit28 , CustomDate28 , CustomNumber28 , CustomReferenceItemID28 , CustomText29 , CustomBit29 , CustomDate29 , CustomNumber29 , CustomReferenceItemID29 , CustomText30 , CustomBit30 , CustomDate30 , CustomNumber30 , CustomReferenceItemID30 , CustomText31 , CustomBit31 , CustomDate31 , CustomNumber31 , CustomReferenceItemID31 , CustomText32 , CustomBit32 , CustomDate32 , CustomNumber32 , CustomReferenceItemID32 , CustomText33 , CustomBit33 , CustomDate33 , CustomNumber33 , CustomReferenceItemID33 , CustomText34 , CustomBit34 , CustomDate34 , CustomNumber34 , CustomReferenceItemID34 , CustomText35 , CustomBit35 , CustomDate35 , CustomNumber35 , CustomReferenceItemID35 , CustomText36 , CustomBit36 , CustomDate36 , CustomNumber36 , CustomReferenceItemID36 , CustomText37 , CustomBit37 , CustomDate37 , CustomNumber37 , CustomReferenceItemID37 , CustomText38 , CustomBit38 , CustomDate38 , CustomNumber38 , CustomReferenceItemID38 , CustomText39 , CustomBit39 , CustomDate39 , CustomNumber39 , CustomReferenceItemID39 , CustomText40 , CustomBit40 , CustomDate40 , CustomNumber40 , CustomReferenceItemID40 , CustomText41 , CustomBit41 , CustomDate41 , CustomNumber41 , CustomReferenceItemID41 , CustomText42 , CustomBit42 , CustomDate42 , CustomNumber42 , CustomReferenceItemID42 , CustomText43 , CustomBit43 , CustomDate43 , CustomNumber43 , CustomReferenceItemID43 , CustomText44 , CustomBit44 , CustomDate44 , CustomNumber44 , CustomReferenceItemID44 , CustomText45 , CustomBit45 , CustomDate45 , CustomNumber45 , CustomReferenceItemID45 , CustomText46 , CustomBit46 , CustomDate46 , CustomNumber46 , CustomReferenceItemID46 , CustomText47 , CustomBit47 , CustomDate47 , CustomNumber47 , CustomReferenceItemID47 , CustomText48 , CustomBit48 , CustomDate48 , CustomNumber48 , CustomReferenceItemID48 , CustomText49 , CustomBit49 , CustomDate49 , CustomNumber49 , CustomReferenceItemID49 , CustomText50 , CustomBit50 , CustomDate50 , CustomNumber50 , CustomReferenceItemID50 FROM Inspection i INNER JOIN InspectionType it On i.InspectionTypeID = it.ID INNER JOIN SystemUser CreatedUser On CreatedUser.ID = i.CreatedUserID INNER JOIN Contract c On i.ContractID = c.ID INNER JOIN Region reg On c.RegionID = reg.ID INNER JOIN InspectionLocation il ON il.InspectionID = i.ID LEFT OUTER JOIN Asset a On i.AssetID = a.ID LEFT OUTER JOIN SystemUser AssignedUser On AssignedUser.ID = i.AssignedUserID LEFT OUTER JOIN Road r On i.RoadID = r.ID LEFT OUTER JOIN AssetType at On it.AssetTypeID = at.ID LEFT OUTER JOIN Point startPoint On i.StartPointID = startPoint.ID LEFT OUTER JOIN Point endPoint On i.EndPointID = endPoint.ID LEFT OUTER JOIN CapitalWork cw On i.CapitalWorkID = cw.ID CROSS APPLY dbo.GetNameForUser_Inline(i.CreatedUserID, i.ContractID, #currentCompanyID, 0) As CreatedUserName OUTER APPLY dbo.GetNameForUser_Inline(i.AssignedUserID, i.ContractID, #currentCompanyID, 0) As AssignedUserName OUTER APPLY ( SELECT TOP 1 SectionName FROM ContractRoad cr WHERE cr.RoadID = r.ID AND cr.ContractID = i.ContractID AND ((i.Carriageway = 0 AND i.ChainageFrom < cr.ChainageToForward AND cr.ChainageFromForward < i.ChainageTo) OR (i.Carriageway = 1 AND i.ChainageFrom < cr.ChainageFromReverse AND cr.ChainageToReverse < i.ChainageTo)) AND cr.Status = #status ) cr OUTER APPLY ( SELECT count(SectionName) as SectionCount FROM ContractRoad cr WHERE cr.RoadID = r.ID AND cr.ContractID = i.ContractID AND ((i.Carriageway = 0 AND i.ChainageFrom < cr.ChainageToForward AND cr.ChainageFromForward < i.ChainageTo) OR (i.Carriageway = 1 AND i.ChainageFrom < cr.ChainageFromReverse AND cr.ChainageToReverse < i.ChainageTo)) AND cr.Status = #status ) sections OUTER APPLY ( SELECT TOP 1 StatusDate, LocalTime.LocalTime as StatusDateLocal FROM InspectionStatus CROSS APPLY tzdb.UtcToLocal_Inline(StatusDate, 'Australia/Sydney') as LocalTime WHERE Status = #status AND InspectionID = i.ID AND InspectionStatus = 1 ORDER BY StatusDate ) startStatus OUTER APPLY ( SELECT TOP 1 StatusDate, LocalTime.LocalTime as StatusDateLocal FROM InspectionStatus CROSS APPLY tzdb.UtcToLocal_Inline(StatusDate, 'Australia/Sydney') as LocalTime WHERE Status = #status AND InspectionID = i.ID AND InspectionStatus = 5 ORDER BY StatusDate ) completedStatus OUTER APPLY ( SELECT TOP 1 ID, StatusDate, InspectionStatus, ModifiedDeviceID, ModifiedUserID FROM InspectionStatus WHERE Status = #status AND InspectionStatus.InspectionStatus <> 7 AND InspectionID = i.ID ORDER BY CASE WHEN InspectionStatus.InspectionStatus = 6 THEN 1 ELSE 0 END, StatusDate DESC ) latestStatus OUTER APPLY ( SELECT COUNT(ID) As Total FROM Job WHERE InspectionID = i.ID AND ParentJobID IS NULL AND Status = #status ) JobCount OUTER APPLY ( SELECT i2.ID FROM dbo.Inspection i2 WHERE i2.OtherDirectionInspectionID = i.ID AND Status = #status ) OtherDirectionInspection OUTER APPLY ( SELECT TOP 1 ir.Name FROM InspectionRoute ir INNER JOIN InspectionGroup ig ON ig.InspectionRouteID = ir.ID WHERE ir.Status = 0 AND ir.CompanyID = 43 AND ig.Status = 0 AND ig.ID = i.InspectionGroupID ) InspectionRouteName WHERE i.Status = 0 AND i.ParentInspectionID IS NULL AND il.Status = 0 --AND ((i.AssetID IS NOT NULL AND i.AssetID NOT IN (SELECT ID FROM #notPreferredAssetIDsTable)) OR i.AssetID IS NULL) AND it.Category <> 2 AND c.IsArchived = 0 AND it.AssetTypeID = 3 AND a.AssetTypeID IS NOT NULL AND a.AssetTypeID = 3 AND i.InspectionTypeID IN (3) AND i.ContractID IN (90,118) AND (latestStatus.InspectionStatus IN (5,6,1,2,3) OR completedStatus.StatusDate IS NOT NULL OR latestStatus.InspectionStatus IS NULL) AND ((completedStatus.StatusDateLocal >= '2021-02-01 00:00:00' AND completedStatus.StatusDateLocal <= '2021-02-05 23:59:59') OR (i.PlannedDate >= '2021-02-01 00:00:00' AND i.PlannedDate <= '2021-02-05 23:59:59') OR (i.CreatedDate >= '2021-02-01 00:00:00' AND i.CreatedDate <= '2021-02-05 23:59:59' AND (latestStatus.InspectionStatus not in (6, 5) OR latestStatus.InspectionStatus IS NULL))) AND ((CASE WHEN i.AssignedUserID IS NULL THEN CreatedUser.CompanyID ELSE AssignedUser.CompanyID END = 43) OR (CASE WHEN i.AssignedUserID IS NULL THEN CreatedUser.CompanyID ELSE AssignedUser.CompanyID END = 1 AND i.ContractID IN (90,118)) OR (CASE WHEN i.AssignedUserID IS NULL THEN CreatedUser.CompanyID ELSE AssignedUser.CompanyID END = 79 AND i.ContractID IN (90)) OR (CASE WHEN i.AssignedUserID IS NULL THEN CreatedUser.CompanyID ELSE AssignedUser.CompanyID END = 80 AND i.ContractID IN (90)) OR (CASE WHEN i.AssignedUserID IS NULL THEN CreatedUser.CompanyID ELSE AssignedUser.CompanyID END = 81 AND i.ContractID IN (90)) OR (CASE WHEN i.AssignedUserID IS NULL THEN CreatedUser.CompanyID ELSE AssignedUser.CompanyID END = 82 AND i.ContractID IN (90))) ORDER BY ID OFFSET #pageSize * (#pageNumber - 1) ROWS FETCH NEXT #pageSize ROWS ONLY OPTION (RECOMPILE)
Find maximum value from an ArrayList
I created an ArrayList function signature to find all the maximum household incomes from year 1984 to 2016. Now I wanted to find the maximum value from that ArrayList. I tried to use max() but it didn't work out. I also tried to use Collections.max as well. //Data string [] year = {"1984" , "1987" , "1989" , "1992" , "1995" , "1997" , "1999" , "2002" , "2004" , "2007" , "2009" , "2012" , "2014" , "2016"}; float [] [] HHI = { {1065.00 , 690.00 , 625.00 , 1040.00 , 1039.00 , 960.00 , 1183.00 , 883.00 , 692.00 , 1590.00 , 756.00 , 1212.00 , 1033.00 , 1920.00}, {1060.00 , 718.00 , 667.00 , 1034.00 , 908.00 , 900.00 , 1130.00 , 863.00 , 711.00 , 1558.00 , 694.00 , 1116.00 , 1141.00 , 1790.00}, {1150.00 , 749.00 , 712.00 , 1084.00 , 1083.00 , 961.00 , 1326.00 , 973.00 , 817.00 , 1658.00 , 759.00 , 1264.00 , 1190.00 , 1824.00}, {1713.00 , 1048.00 , 907.00 , 1459.00 , 1380.00 , 1262.00 , 1821.00 , 1274.00 , 1040.00 , 2280.00 , 939.00 , 1490.00 , 1480.00 , 2429.00}, {2137.71 , 1295.42 , 1090.80 , 1843.21 , 1766.66 , 1435.73 , 2224.63 , 1436.11 , 1157.94 , 3161.68 , 1116.97 , 1646.69 , 1885.97 , 3371.01}, {2772.00 , 1590.00 , 1249.00 , 2276.00 , 2378.00 , 1632.00 , 3130.00 , 1940.00 , 1507.00 , 4006.00 , 1497.00 , 2057.43 , 2241.78 , 4768.00}, {2645.57 , 1612.00 , 1314.00 , 2260.00 , 2335.00 , 1482.00 , 3128.00 , 1743.00 , 1431.00 , 3702.48 , 1599.00 , 1905.00 , 2275.89 , 4104.61}, {2963.00 , 1966.00 , 1674.00 , 2650.00 , 2739.00 , 1991.00 , 3496.00 , 2153.00 , 2006.00 , 4406.00 , 1837.00 , 2406.32 , 2515.00 , 4930.00}, {3076.00 , 2126.00 , 1829.00 , 2791.00 , 2886.00 , 2410.00 , 3531.00 , 2207.00 , 2046.00 , 5175.00 , 1984.00 , 2487.00 , 2725.00 , 5011.00}, {3456.63 , 2407.79 , 2142.81 , 3421.30 , 3336.20 , 2994.68 , 4003.73 , 2545.07 , 2540.67 , 5579.88 , 2463.48 , 2837.00 , 3349.01 , 5322.00}, {3835.00 , 2667.00 , 2536.00 , 4184.00 , 3540.00 , 3279.00 , 4407.00 , 2809.00 , 2617.00 , 5962.00 , 3017.00 , 3102.00 , 3581.00 , 5488.00}, {4658.00 , 3425.00 , 3168.00 , 4759.00 , 4576.00 , 3745.00 , 5055.00 , 3548.00 , 3538.00 , 7023.00 , 3967.00 , 4013.00 , 4293.00 , 8586.00}, {6207.00 , 4478.00 , 3715.00 , 6046.00 , 5271.00 , 4343.00 , 5993.00 , 4268.00 , 4445.00 , 8252.00 , 4816.00 , 4879.00 , 4934.00 , 10629.00}, {6928.00 , 4971.00 , 4214.00 , 6849.00 , 5887.00 , 5012.00 , 6771.00 , 5065.00 , 4998.00 , 9463.00 , 5776.00 , 5354.00 , 5387.00 , 11692.00} }; //Function max ArrayList <Int> Max (float [][] data, string [] time){ ArrayList <Int> maximum = new ArrayList <Int> (); for(i=0; i<time.length; i++){ maximum.add(max(data[i])); } return maximum; } ArrayList <Int> MaxHHI=Max(HHI, year); Is there anyway to get the maximum value from the ArrayList MaxHHI ? Appreciate the help. Thanks a million.
What is the best way to delete bulk amount of rows in SQL Server 2008 R2
Here I am using a query, "delete from Table where id in" ({0})", string.Join(",", Key.ToArray()) Is any other best way to speed up this process? Note : I used indexes. Example : I used SQL profiller to traced it query is: delete from [Table] where Key in (27267 , 27268 , 27269 , 27270 , 27271 , 27272 , 27273 , 27274 , 27275 , 27276 , 27277 , 27278 , 27279 , 27280 , 27281 , 27282 , 27283 , 27284 , 27285 , 27286 , 27287 , 27288 , 27289 , 27290 , 27291 , 27292 , 27293 , 27294 , 27295 , 27296 , 27297 , 27298 , 27299 , 27300 , 27301 , 27302 , 27303 , 27304 , 27305 , 27306 , 27307 , 27308 , 27309 , 27310 , 27311 , 27312 , 27313 , 27314 , 27315 , 27316 , 27317 , 27318 , 27319 , 27320 , 27321 , 27322 , 27323 , 27324 , 27325 , 27326 , 27327 , 27328 , 27329 , 27330 , 27331 , 27332 , 27333 , 27334 , 27335 , 27336 , 27337 , 27338 , 27339 , 27340 , 27341 , 27342 , 27343 , 27344 , 27345 , 27346 , 27347 , 27348 , 27349 , 27350 , 27351 , 27352 , 27353 , 27354 , 27355 , 27356 , 27357 , 27358 , 27359 , 27360 , 27361 , 27362 , 27363 , 27364 , 27365 , 27366 , 27367 , 27368 , 27369 , 27370 , 27371 , 27372 , 27373 , 27374 , 27375 , 27376 , 27377 , 27378 , 27379 , 27380 , 27381 , 27382 , 27383 , 27384 , 27385 , 27386 , 27387 , 27388 , 27389 , 27390 , 27391 , 27392 , 27393 , 27394 , 27395 , 27396 , 27397 , 27398 , 27399 , 27400 , 27401 , 27402 , 27403 , 27404 , 27405 , 27406 , 27407 , 27408 , 27409 , 27410 , 27411 , 27412 , 27413 , 27414 , 27415 , 27416 , 27417 , 27418 , 27419 , 27420 , 27421 , 27422 , 27423 , 27424 , 27425 , 27426 , 27427 , 27428 , 27429 , 27430 , 27431 , 27432 , 27433 , 27434 , 27435 , 27436 , 27437 , 27438 , 27439 , 27440 , 27441 , 27442 , 27443 , 27444 , 27445 , 27446 , 27447 , 27448 , 27449 , 27450 , 27451 , 27452 , 27453 , 27454 , 27455 , 27456 , 27457 , 27458 , 27459 , 27460 , 27461 , 27462 , 27463 , 27464 , 27465 , 27466 , 27467 , 27468 , 27469 , 27470 , 27471 , 27472 , 27473 , 27474 , 27475 , 27476 , 27477 , 27478 , 27479 , 27480 , 27481 , 27482 , 27483 , 27484 , 27485 , 27486 , 27487 , 27488 , 27489 , 27490 , 27491 , 27492 , 27493 , 27494 , 27495 , 27496 , 27497 , 27498 , 27499 , 27500 , 27501 , 27502 , 27503 , 27504 , 27505 , 27506 , 27507 , 27508 , 27509 , 27510 , 27511 , 27512 , 27513 , 27514 , 27515 , 27516 , 27517 , 27518 , 27519 , 27520 , 27521 , 27522 , 27523 , 27524 , 27525 , 27526 , 27527 , 27528 , 27529 , 27530 , 27531 , 27532 , 27533 , 27534 , 27535 , 27536 , 27537 , 27538 , 27539 , 27540 , 27541 , 27542 , 27543 , 27544 , 27545 , 27546 , 27547 , 27548 , 27549 , 27550 , 27551 , 27552 , 27553 , 27554 , 27555 , 27556 , 27557 , 27558 , 27559 , 27560 , 27561 , 27562 , 27563 , 27564 , 27565 , 27566 , 27567 , 27568 , 27569 , 27570 , 27571 , 27572 , 27573 , 27574 , 27575 , 27576 , 27577 , 27578 , 27579 , 27580 , 27581 , 27582 , 27583 , 27584 , 27585 , 27586 , 27587 , 27588 , 27589 , 27590 , 27591 , 27592 , 27593 , 27594 , 27595 , 27596 , 27597 , 27598 , 27599 , 27600 , 27601 , 27602 , 27603 , 27604 , 27605 , 27606 , 27607 , 27608 , 27609 , 27610 , 27611 , 27612 , 27613 , 27614 , 27615 , 27616 , 27617 , 27618 , 27619 , 27620 , 27621 , 27622 , 27623 , 27624 , 27625 , 27626 , 27627 , 27628 , 27629 , 27630 , 27631 , 27632 , 27633 , 27634 , 27635 , 27636 , 27637 , 27638 , 27639 , 27640 , 27641 , 27642 , 27643 , 27644 , 27645 , 27646 , 27647 , 27648 , 27649 , 27650 , 27651 , 27652 , 27653 , 27654 , 27655 , 27656 , 27657 , 27658 , 27659 , 27660 , 27661 , 27662 , 27663 , 27664 , 27665 , 27666 , 27667 , 27668 , 27669 , 27670 , 27671 , 27672 , 27673 , 27674 , 27675 , 27676 , 27677 , 27678 , 27679 , 27680 , 27681 , 27682 , 27683 , 27684 , 27685 , 27686 , 27687 , 27688 , 27689 , 27690 , 27691 , 27692 , 27693 , 27694 , 27695 , 27696 , 27697 , 27698 , 27699 , 27700 , 27701 , 27702 , 27703 , 27704 , 27705 , 27706 , 27707 , 27708 , 27709 , 27710 , 27711 , 27712 , 27713 , 27714 , 27715 , 27716 , 27717 , 27718 , 27719 , 27720 , 27721 , 27722 , 27723 , 27724 , 27725 , 27726 , 27727 , 27728 , 27729 , 27730 , 27731 , 27732 , 27733 , 27734 , 27735 , 27736 , 27737 , 27738 , 27739 , 27740 , 27741 , 27742 , 27743 , 27744 , 27745 , 27746 , 27747 , 27748 , 27749 , 27750 , 27751 , 27752 , 27753 , 27754 , 27755 , 27756 , 27757 , 27758 , 27759 , 27760 , 27761 , 27762 , 27763 , 27764 , 27765 , 27766) Here I am deleting 500 records, its takes 6553 milliseconds duration.
This option will likely speed up the delete, but may not speed up the overall SQL processing time. So with that caveat... You could first insert the keys you want to delete into a temporary variable (or temp table). Then, you could use a join in the delete clause so you don't have to use an IN clause. Ex. DECLARE #MyTableVar table(id int) INSERT INTO #MyTableVar (id) VALUES (27267) //Repeat insert for other values, or use a different mechanism to insert keys //into the temp table/table variable. //Since that's not the focus of this question, I won't go into details on this //portion. ... DELETE FROM [your table] FROM [your table] T1 INNER JOIN #MyTableVar T2 ON T1.id= T2.id Obviously the Inserts will take time, so if you are concerned with the overall time of the transaction this may not help. But if making the delete portion faster is the primary concern (to minimize lock time, for example), then this may still be useful. See http://msdn.microsoft.com/en-us/library/ms189835.aspx for usage examples of a DELETE statement that includes a JOIN and http://msdn.microsoft.com/en-us/library/ms188927.aspx for info on a table variable.
Combining NOT EXISTS with INSERT ... SELECT
The following code inserts a new row into a table based on a value in another row but it also allows duplicate data. I want to use NOT EXISTS so that if there is already a row with that value then it won't insert another one, but not sure how to integrate this. INSERT INTO [Grading].[dbo].[tblObservations] ([FormID] ,[Data] ,[UserID] ,[DateOfObservation] ,[Final] ,[ValidTo] ,[ID_Grading] ,[ID_ObservationKind] ,[Created] ,[Modified] ,[RowVersion]) SELECT [FormID] ,'0' ,[UserID] ,[DateOfObservation] ,[Final] ,[ValidTo] ,[ID_Grading] ,40 ,[Created] ,[Modified] ,[RowVersion] FROM [Grading].[dbo].[tblObservations] WHERE [ID_ObservationKind] = 39 AND [Data] = 'No' AND [Final] = 1 Any help appreciated.
Try this one - USE [Grading] INSERT INTO [dbo].[tblObservations] ( FormID , Data , UserID , DateOfObservation , Final , ValidTo , ID_Grading , ID_ObservationKind , Created , Modified , [RowVersion] ) SELECT t.FormID , '0' , t.UserID , t.DateOfObservation , t.Final , t.ValidTo , t.ID_Grading , 40 , t.Created , t.Modified , t.[RowVersion] FROM dbo.tblObservations t LEFT JOIN dbo.tblObservations t2 ON t.FormID = t2.FormID AND t2.Data = '0' AND t.UserID = t2.UserID AND t.DateOfObservation = t2.DateOfObservation AND t.Final = t2.Final AND t.ValidTo = t2.ValidTo AND t.ID_Grading = t2.ID_Grading AND t2.ID_ObservationKind = 40 AND t.Created = t2.Created AND t.Modified = t2.Modified AND t.[RowVersion] = t2.[RowVersion] WHERE t.ID_ObservationKind = 39 AND t.Data = 'No' AND t.Final = 1 AND t2.FormID IS NULL Or try this - INSERT INTO [dbo].[tblObservations] ( FormID , Data , UserID , DateOfObservation , Final , ValidTo , ID_Grading , ID_ObservationKind , Created , Modified , [RowVersion] ) SELECT t.FormID , '0' , t.UserID , t.DateOfObservation , t.Final , t.ValidTo , t.ID_Grading , 40 , t.Created , t.Modified , t.[RowVersion] FROM dbo.tblObservations t WHERE t.ID_ObservationKind = 39 AND t.Data = 'No' AND t.Final = 1 AND NOT EXISTS( SELECT 1 FROM dbo.tblObservations t2 WHERE t2.ID_ObservationKind = 39 AND t2.Data = 'No' AND t2.Final = 1 )
You can try IF NOT EXISTS ( SELECT 1 FROM [Grading].[dbo].[tblObservations] WHERE [ID_ObservationKind] = 39 AND [Data] = 'No' AND [Final] = 1 ) BEGIN -- Insert script END
You're looking to do a "merge" or "upsert" in SQL. Depending on your database, there are a number of ways to do this. If you are using MySQL, you'll want to use it's INSERT... ON DUPLICATE KEY UPDATE syntax.
how to fix sql case syntax
work on sql-server-2005 SELECT A.specific_customization_id , A.customization_id , A.customization_value , A.customization_price , A.customization_cost , A.customization_code , A.customization_check , A.sort_order , A.option_code , A.product_id , A.image_1 , A.image_2 , A.MainProductID , A.customization_product_id , A.inactive , A.customization_description , A.customization_select , A.UOM , A.allow_recur , auto_reorder = CASE A.MainProductID --it's an int type column WHEN NULL THEN A.allow_recur ELSE ( SELECT allow_recur FROM wc_product WHERE product_id = A.MainProductID ) END FROM dbo.wc_product_specific A WHERE A.product_id = 1133 Using the above query I want to set auto_reorder column value .want to set two login When My MainProductID column is null I want to set allow_recur column value When it’s not null I want to set another table column value My logic 2) works fine ,fail to understand why does 1) not work? Help me to fix the problem? If have any query plz ask thanks in advance.
Does this work:? SELECT A.specific_customization_id , A.customization_id , A.customization_value , A.customization_price , A.customization_cost , A.customization_code , A.customization_check , A.sort_order , A.option_code , A.product_id , A.image_1 , A.image_2 , A.MainProductID , A.customization_product_id , A.inactive , A.customization_description , A.customization_select , A.UOM , A.allow_recur , A.auto_reorder = CASE --it's an int type column WHEN A.MainProductID IS NULL THEN A.allow_recur ELSE B.allow_recur END FROM dbo.wc_product_specific A left outer join wc_product B on B.product_id = A.MainProductID WHERE A.product_id = 1133