我在创建日期模拟查询时遇到问题。
dkd_kd mr LAST_EVENT_PROC DayOfLAST_EVENT_PROC BusinessDay
------------------------------------------------------------------------
C01R0307 bma16 5 Monday 5
C01R0304 bma16 4 Sunday 5
C01R0305 bma12 4 Sunday 5
C01R0313 bma12 3 Saturday 3
我已经创建了一个功能,可以将星期日作为工作日跳过,但我需要BusinessDay
中存在LAST_EVENT_PROC
并且具有相同的'mr'然后是BusinessDay + 1
这是我的疑问:
SELECT
dkd_kd,
mr,
next_tgl_baca,
datepart(day,LAST_EVENT_PROC) as LAST_EVENT_PROC,
DATENAME(weekday, LAST_EVENT_PROC) AS DayOfLAST_EVENT_PROC,
DATEPART(day, DATEADD(dd, CASE
WHEN DATEDIFF(dd, 0, LAST_EVENT_PROC) % 7 > 5
THEN 7 - DATEDIFF(dd, 0, LAST_EVENT_PROC) % 7
WHEN DATENAME(weekday, LAST_EVENT_PROC) = 'Monday'
THEN 7 - DATEDIFF(dd, 1, LAST_EVENT_PROC) % 7
ELSE 0 END, LAST_EVENT_PROC)) AS NextBusinessDay
FROM
tm_perkiraan_konsumsi
JOIN
tr_kode_rute ON tm_perkiraan_konsumsi.dkd_kd = tr_kode_rute.rute
WHERE
next_tgl_baca <> '' AND pk_id LIKE '%6:02PM%';