如何使用案例和交叉连接实现新方法以改善响应时间?代码如下

时间:2018-02-20 18:13:06

标签: sql-server ssis ssms case-when cross-join

以下代码需要7分钟才能完成,代码是SSIS ETL转换步骤的一部分。 还有另一种方法可以做到这一点吗? 表1与作为日历表的表2进行比较。 SQL代码如下:

选择

ROW_NUMBER() OVER (ORDER BY Date_key asc) ID,

SUM(CASE WHEN C.DATE_FIELD1 >= B.DATE_FIELD_2
AND C.DATE_FIELD_1 <= B.DATE_FIELD_3
AND C.ISHOLIDAY =0
THEN C.ISWEEKDAY  END) -1 AS RESULT...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_2 
AND C.DATE_FIELD_1 <= B.[DATE_FIELD_4]
AND C.isHoliday = 0 
THEN C.isWeekday END) -1 AS RESULT...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_2
AND C.DATE_FIELD_1 <= B.DATE_FIELD_5 
AND C.isHoliday = 0
THEN C.isWeekday END) -1 RESULT...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_2 
AND C.DATE_FIELD_1 <= B.DATE_FIELD_6 
AND C.isHoliday = 0
THEN C.isWeekday END)-1 AS RESULT...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_5
AND C.DATE_FIELD_1 <= B.DATE_FIELD_3 
AND C.isHoliday = 0
THEN C.isWeekday END) -1 AS RESULT...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_5
AND C.DATE_FIELD_1 <= B.DATE_FIELD_4
AND C.isHoliday =0
THEN C.isWeekday END) -1 AS RESULT...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_2
AND C.DATE_FIELD_1 <= B.DATE_FIELD_7 
AND C.isHoliday = 0
THEN C.isWeekday END) -1 AS RESULT...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_8
AND C.DATE_FIELD_1 <= B.DATE_FIELD_7 
AND C.isHoliday = 0
THEN C.isWeekday END) -1 AS RESULT...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_9
AND C.DATE_FIELD_1 <= B.DATE_FIELD_10
AND C.isHoliday = 0 
THEN C.isWeekday END)-1 AS RESULT...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_9 
AND C.DATE_FIELD_1 <= B.DATE_FIELD_11
AND C.isHoliday = 0 
THEN C.isWeekday END) -1 AS RESULT...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_9
AND C.DATE_FIELD_1 <= B.DATE_FIELD_12
AND C.isHoliday = 0
THEN C.isWeekday END) -1 AS RESULT...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_3 
AND C.DATE_FIELD_1 <= GETDATE()
AND C.isHoliday = 0
THEN C.ISWEEKDAY END)-1 AS 'RESULT...',

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_3
AND C.DATE_FIELD_1 <= B.DATE_FIELD_10
AND C.isHoliday = 0 
THEN C.isWeekday END) -1 AS RESULT...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_3
AND C.DATE_FIELD_1 <= CAST(B.DATE_FIELD_8 AS DATE)
AND C.isHoliday = 0
THEN C.isWeekday END) -1 AS RESULT...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_3
AND C.DATE_FIELD_1 <= B.DATE_FIELD_12
AND C.isHoliday = 0
THEN C.isWeekday END) -1 AS RESULT...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_3
AND C.DATE_FIELD_1 <= CAST(B.DATE_FIELD_13 AS DATE)
AND C.isHoliday = 0 
THEN C.isWeekday END) -1 AS result...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_4
AND C.DATE_FIELD_1 <= B.DATE_FIELD_10
AND C.isHoliday = 0
THEN C.isWeekday END ) -1 AS result...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_4
AND C.DATE_FIELD_1 <= CAST(B.DATE_FIELD_8 AS DATE)
AND C.isHoliday =0
THEN C.isWeekday END) -1 AS result...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_4
AND C.DATE_FIELD_1 <= B.DATE_FIELD_12
AND C.ISHOLIDAY = 0
THEN C.isWeekday END ) -1 AS result...,

SUM(CASE WHEN C.DATE_FIELD_1 >=B.DATE_FIELD_4
AND C.DATE_FIELD_1 <= CAST(B.DATE_FIELD_13 AS DATE)
AND C.isHoliday = 0
THEN C.isWeekday END ) -1 AS result...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_14
AND C.DATE_FIELD_1 <= B.DATE_FIELD_15
AND C.isHoliday = 0
THEN C.isWeekday END) -1 AS result...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_2
AND C.DATE_FIELD_1 <= GETDATE()
AND C.isHoliday =0
THEN C.isWeekday END) -1 AS result...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_15
AND C.DATE_FIELD_1 <= GETDATE()
AND C.isHoliday =0
THEN C.isWeekday END) -1 AS result,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_16
AND C.DATE_FIELD_1 <= GETDATE()
AND C.isHoliday =0
THEN C.isWeekday END) -1 AS result...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_17
AND C.DATE_FIELD_1 <= GETDATE()
AND C.isHoliday =0
THEN C.isWeekday END) -1 AS result...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_3
AND C.DATE_FIELD_1 <= CAST(B.DATE_FIELD_18 AS DATE)
AND C.isHoliday =0
THEN C.isWeekday END) -1 AS result...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_4
AND C.DATE_FIELD_1 <= B.DATE_FIELD_18
AND C.isHoliday = 0 
THEN C.isWeekday END) -1 AS result...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_3
AND C.DATE_FIELD_1 <= B.DATE_FIELD_19
AND C.isHoliday =0
THEN C.isWeekday END)-1 AS result...,

SUM(CASE WHEN C.DATE_FIELD_1 >= B.DATE_FIELD_3
AND C.DATE_FIELD_1 <= B.DATE_FIELD_11
AND C.isHoliday = 0
THEN C.isWeekday END) -1 AS result...,

SUM(CASE WHEN C.DATE_FIELD_1 >=B.DATE_FIELD_4
AND C.DATE_FIELD_1 <= B.DATE_FIELD_11 
AND C.isHoliday = 0
THEN C.isWeekday END)-1 AS  result...,

SUM (CASE WHEN C.DATE_FIELD_1 >= GETDATE()
    AND C.DATE_FIELD_1 <= B.DATE_FIELD_6
    AND C.DATE_FIELD_20 =0 THEN 1 END)  AS result...

FROM DIM_DATE B
CROSS JOIN DIM_DATE_2 C
GROUP BY Date_Key
order by Date_Key asc

注意: Dim_date中有40k记录(表1) 和Dim_date 2中的7k记录(表2)。 有什么功能可以帮助我减少响应时间吗?

0 个答案:

没有答案