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 * STEP1.T_CUR_BK_BAL_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INAL_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_MTD_PRE.PMT_FREQ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_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.
I have the following View:
So the question is,How to Improve SQL readability of this query?
SELECT t1.Id ,
t1.HeaderRef ,
t1.PartRef ,
t1.SLRef ,
t1.DL ,
t1.DL2 ,
t1.DL3 ,
t1.DL4 ,
t1.DL5 ,
t1.DL6 ,
t1.DL7 ,
t1.ItemRef ,
t1.CalItemRef ,
t1.RowNum ,
t1.UnitType ,
t1.UnitTypeTitle ,
t1.UnitRef ,
t1.Ratio ,
t1.Value ,
t1.MasterValue ,
t1.ItemDate ,
t1.Descript ,
t1.Year ,
t1.BranchRef ,
t1.StockRef ,
t1.OpositStockRef ,
t1.DocTypeRef ,
t1.BaseDocTypeRef ,
t1.Creator ,
t1.Confirmer ,
t1.Num ,
t1.State ,
t1.DocDate ,
t1.FCRef ,
t1.FCTitle ,
t1.PartTitle ,
t1.PartCode ,
t1.SLCode ,
t1.DL1Code ,
t1.DL2Code ,
t1.DL3Code ,
t1.DL4Code ,
t1.DL5Code ,
t1.DL6Code ,
t1.DL7Code ,
t1.UnitTitle ,
t1.PartUnitTitle ,
( ( AVG(t1.RealPrice) + SUM(t1.CorrectPrice) ) / t1.Value ) AS CorrectRate ,
AVG(t1.RealPrice) + SUM(t1.CorrectPrice) AS CorrectPrice ,
MAX(t1.CorrectAccDocHeaderRef) AS CorrectAccDocHeaderRef ,
1 AS CorrectCreator ,
1 AS CorrectConfirmer ,
MAX(t1.CorrectCreateDate) AS CorrectCreateDate ,
GETDATE() AS CorrectConfirmDate ,
CorrectFCRate = ( SELECT MAX(rr.FCRate)
FROM Stc.StcDocRate AS rr
WHERE rr.ItemRef = t1.Id
AND rr.PriceType = '2'
AND CreateDate = MAX(t1.CorrectCreateDate)
) ,
CorrectFCVal = ( SELECT MAX(rr.FCVal)
FROM Stc.StcDocRate AS rr
WHERE rr.ItemRef = t1.Id
AND rr.PriceType = '2'
AND CreateDate = MAX(t1.CorrectCreateDate)
),
CorrectDescript = ( SELECT MAX(Descript)
FROM Stc.StcDocRate AS rr
WHERE rr.ItemRef = t1.Id
AND rr.PriceType = '2'
AND CreateDate = MAX(t1.CorrectCreateDate)
) ,
AVG(t1.RealPrice) / t1.Value AS RealRate ,
AVG(t1.RealPrice) AS RealPrice ,
MAX(t1.RealAccDocHeaderRef) AS RealAccDocHeaderRef ,
1 AS RealCreator ,
1 AS RealConfirmer ,
GETDATE() AS RealCreateDate ,
GETDATE() AS RealConfirmDate ,
AVG(t1.RealFCRate) AS RealFCRate ,
AVG(t1.RealFCVal) AS RealFCVal ,
AVG(t1.CaledPrice) / t1.Value AS CaledRate ,
AVG(t1.CaledPrice) AS CaledPrice ,
MAX(t1.CaledAccDocHeaderRef) AS CaledAccDocHeaderRef ,
1 AS CaledCreator ,
1 AS CaledConfirmer ,
GETDATE() AS CaledCreateDate ,
GETDATE() AS CaledConfirmDate ,
AVG(t1.CaledFCRate) AS CaledFCRate ,
AVG(t1.CaledFCVal) AS CaledFCVal ,
t1.ItemNum ,
t1.BasePartCode ,
t1.BasePartTitle ,
t1.DlRefHeader ,
t1.SLRefHeader,
t1.ProductionValue ,
t1.FormulaRef ,
t1.StcDocReqRef
FROM ( SELECT item.Id ,
item.HeaderRef ,
item.PartRef ,
item.SLRef ,
item.DL ,
item.DL2 ,
item.DL3 ,
item.DL4 ,
item.DL5 ,
item.DL6 ,
item.DL7 ,
item.ItemRef ,
item.CalItemRef ,
item.RowNum ,
item.UnitType ,
UnitTypeTitle = CASE item.UnitType
WHEN '1' THEN N'اصلی'
WHEN '2' THEN N'فرعی'
END ,
item.UnitRef ,
item.Ratio ,
item.Value ,
item.MasterValue ,
item.ItemDate ,
item.Descript ,
item.StcDocReqRef ,
item.Year ,
item.BranchRef ,
item.StockRef ,
item.OpositStockRef ,
item.DocTypeRef ,
item.BaseDocTypeRef ,
item.Creator ,
item.Confirmer ,
item.Num ,
item.State ,
item.DocDate ,
item.FCRef ,
fc.Title AS FCTitle ,
item.ProductionValue ,
item.FormulaRef ,
p.Title AS PartTitle ,
p.Code AS PartCode ,
sl.SLCode AS SLCode ,
dl1.DLCode AS DL1Code ,
dl2.DLCode AS DL2Code ,
dl3.DLCode AS DL3Code ,
dl4.DLCode AS DL4Code ,
dl5.DLCode AS DL5Code ,
dl6.DLCode AS DL6Code ,
dl7.DLCode AS DL7Code ,
pu.Title AS UnitTitle ,
puslave.Title AS PartUnitTitle ,
CorrectRate.Rate AS CorrectRate ,
CorrectRate.Price AS CorrectPrice ,
CorrectRate.AccDocHeaderRef AS CorrectAccDocHeaderRef ,
CorrectRate.Creator AS CorrectCreator ,
CorrectRate.Confirmer AS CorrectConfirmer ,
CorrectRate.CreateDate AS CorrectCreateDate ,
CorrectRate.ConfirmDate AS CorrectConfirmDate ,
CorrectRate.FCRate AS CorrectFCRate ,
CorrectRate.FCVal AS CorrectFCVal ,
CorrectRate.Descript AS CorrectDescript ,
realRate.Rate RealRate ,
realRate.Price RealPrice ,
realRate.AccDocHeaderRef AS RealAccDocHeaderRef ,
realRate.Creator AS RealCreator ,
realRate.Confirmer AS RealConfirmer ,
realRate.CreateDate AS RealCreateDate ,
realRate.ConfirmDate AS RealConfirmDate ,
realRate.FCRate AS RealFCRate ,
realRate.FCVal AS RealFCVal ,
CaledRate.Rate AS CaledRate ,
CaledRate.Price AS CaledPrice ,
CaledRate.AccDocHeaderRef AS CaledAccDocHeaderRef ,
CaledRate.Creator AS CaledCreator ,
CaledRate.Confirmer AS CaledConfirmer ,
CaledRate.CreateDate AS CaledCreateDate ,
CaledRate.ConfirmDate AS CaledConfirmDate ,
CaledRate.FCRate AS CaledFCRate ,
CaledRate.FCVal AS CaledFCVal ,
ItemNum = CASE header.BaseDocTypeRef
WHEN 2000 THEN refReqItem.Num
WHEN 2002 THEN refOrderItem.Num
WHEN 2003 THEN refFactItem.Num
WHEN 2004 THEN refRetFactItem.Num
WHEN 2010 THEN cmrPostItem.Num
WHEN 2012 THEN cmrRet.Num
WHEN 2016 THEN header.ProductionNum
ELSE refItem.Num
END ,
refPart.Code AS BasePartCode ,
refPart.Title AS BasePartTitle ,
header.DLRef AS DlRefHeader ,
header.SLRef AS SLRefHeader
FROM Stc.StcDocItem AS item
INNER JOIN Stc.Part AS p ON p.Id = item.PartRef
LEFT OUTER JOIN Acc.SL AS sl ON sl.Id = item.SLRef
LEFT OUTER JOIN Acc.DL AS dl1 ON dl1.Id = item.DL
LEFT OUTER JOIN Acc.DL AS dl2 ON dl2.Id = item.DL2
LEFT OUTER JOIN Acc.DL AS dl3 ON dl3.Id = item.DL3
LEFT OUTER JOIN Acc.DL AS dl4 ON dl4.Id = item.DL4
LEFT OUTER JOIN Acc.DL AS dl5 ON dl5.Id = item.DL5
LEFT OUTER JOIN Acc.DL AS dl6 ON dl6.Id = item.DL6
LEFT OUTER JOIN Acc.DL AS dl7 ON dl7.Id = item.DL7
LEFT OUTER JOIN Acc.FC AS fc ON fc.Id = item.FCRef
LEFT OUTER JOIN Stc.PartUnit AS pu ON pu.Id = item.UnitRef
LEFT OUTER JOIN Stc.PartUnit AS puslave ON puslave.Id = p.UnitRef
LEFT OUTER JOIN Stc.StcDocRate AS realRate ON realRate.ItemRef = item.Id
AND realRate.PriceType = '1'
LEFT OUTER JOIN Stc.StcDocRate AS CorrectRate ON CorrectRate.ItemRef = item.Id
AND CorrectRate.PriceType = '2'
LEFT OUTER JOIN Stc.StcDocRate AS CaledRate ON CaledRate.ItemRef = item.Id
AND CaledRate.PriceType = '3'
LEFT OUTER JOIN Stc.StcDocItem AS refItem ON refItem.Id = item.ItemRef
LEFT OUTER JOIN Stc.Part AS refPart ON refItem.PartRef = refPart.Id
LEFT OUTER JOIN Stc.StcDocReqItem AS refReqItem ON refReqItem.Id = item.ItemRef
LEFT OUTER JOIN Sle.SleDocOrderItem AS refOrderItem ON refOrderItem.Id = item.ItemRef
LEFT OUTER JOIN Sle.SleDocFactItem AS refFactItem ON refFactItem.Id = item.ItemRef
LEFT OUTER JOIN Sle.SleDocRetFactItem AS refRetFactItem ON refRetFactItem.Id = item.ItemRef
LEFT OUTER JOIN CMR.CMRIntSheetPostItem AS cmrPostItem ON cmrPostItem.Id = item.ItemRef
LEFT OUTER JOIN CMR.CMRIntReturnPartNonConformingItem
AS cmrRet ON cmrRet.Id = item.ItemRef
LEFT OUTER JOIN Stc.StcDocHeader AS header ON header.Id = item.HeaderRef
) AS t1
Readability of this query is pretty bad. In particular, i had problem to understand.
I would like some feedback how to improve performance and Readability for this case?
1. readability
if you have to get one output select with all this columns, your query is well formed enough
if you can decompose your qry, so split it into smaller logical (or semantically connected) blocks
use views for pivoted output - for example instead of 8x Acc.SL left join create view with pivoted data
use functions or views to remove case statement or subqueries
separate agregations and other data (it seems in your qry is missing group by statement)
2. performance
get query plan
rewrite your query to use existing indexes or add missing indexes
force joins order if it's necessary