在下图中,我试图从每个星期一开始“ 制造日期”这一周,但无法获得所需的结果。
使用的查询:
select distinct dateadd(week, DATEDIFF(week, 0, [ManufactureDate]),0 )sp, [ManufactureDate]
FROM ab
当前结果集:
SP Manufacturing Date
2019-10-14 00:00:00.000 2019-10-13 00:00:00.000
2019-10-14 00:00:00.000 2019-10-14 00:00:00.000
2019-10-14 00:00:00.000 2019-10-15 00:00:00.000
2019-10-14 00:00:00.000 2019-10-16 00:00:00.000
2019-10-14 00:00:00.000 2019-10-17 00:00:00.000
2019-10-14 00:00:00.000 2019-10-18 00:00:00.000
2019-10-21 00:00:00.000 2019-10-20 00:00:00.000
所需结果:
Sp Manufacturing Date
10/14/2019 10/14/2019
10/14/2019 10/15/2019
10/14/2019 10/16/2019
10/14/2019 10/17/2019
10/14/2019 10/18/2019
10/14/2019 10/20/2019
10/21/2019 10/21/2019
图片:
答案 0 :(得分:4)
SELECT
DATEADD(DAY, ((DATEPART(WEEKDAY,[ManufactureDate]) + 5) % 7) * -1, [ManufactureDate]) Sp, [ManufactureDate]
FROM ab
这里是fiddle。
Sp ManufactureDate
2019-10-07 2019-10-13
2019-10-14 2019-10-14
2019-10-14 2019-10-15
2019-10-14 2019-10-16
2019-10-14 2019-10-17
2019-10-14 2019-10-18
2019-10-14 2019-10-19
2019-10-14 2019-10-20
2019-10-21 2019-10-21