定义从期间开始开始的两个日期之间的周数
Define number of week between two dates starting with start of the period
我有,例如:
create table #test ( job int, dateL date)
insert into #test values
(1, '2022-04-01'),
(2, '2022-04-02'),
(3, '2022-04-12'),
(4, '2022-04-27'),
(5, '2022-05-01')
declare
@startdate date = '2022-04-01',
@enddate date = '2022-04-30'
set @startdateweekstart = (select DATEPART(WEEKDAY, @startdate))
SELECT DATEADD( DAY , @startdateweekstart - DATEPART(WEEKDAY, @startdate), CAST (@startdate AS DATE )) [Week_Start_Date]
Select DATEADD( DAY , @startdateweekstart + 6 - DATEPART(WEEKDAY, @startdate), CAST (@startdate AS DATE )) [Week_End_Date]
所以我搜索了解决方案,但真的找到了。不确定我走在正确的轨道上
我需要让我的输出看起来像这样,这样我可以稍后在 ssrs 中按周对它进行分组
job dateL Week
1 '2022-04-01' 1
2 '2022-04-02' 1
3 '2022-04-12' 2
4 '2022-04-27' 4
5 '2022-05-01' 0
其中 0 不属于@startdate 和@enddate 之间的范围。
如能向正确方向踢球,我们将不胜感激!谢谢!
试试这个代码:
SELECT job,dateL,
(
SELECT CASE
WHEN dateL BETWEEN @startdate AND @enddate THEN
(SELECT MAX(v) FROM (VALUES (1), (DATEDIFF(wk, @startdate, dateL))) AS VALUE(v))
ELSE 0
END
)
AS Week
FROM #test;
我有,例如:
create table #test ( job int, dateL date)
insert into #test values
(1, '2022-04-01'),
(2, '2022-04-02'),
(3, '2022-04-12'),
(4, '2022-04-27'),
(5, '2022-05-01')
declare
@startdate date = '2022-04-01',
@enddate date = '2022-04-30'
set @startdateweekstart = (select DATEPART(WEEKDAY, @startdate))
SELECT DATEADD( DAY , @startdateweekstart - DATEPART(WEEKDAY, @startdate), CAST (@startdate AS DATE )) [Week_Start_Date]
Select DATEADD( DAY , @startdateweekstart + 6 - DATEPART(WEEKDAY, @startdate), CAST (@startdate AS DATE )) [Week_End_Date]
所以我搜索了解决方案,但真的找到了。不确定我走在正确的轨道上
我需要让我的输出看起来像这样,这样我可以稍后在 ssrs 中按周对它进行分组
job dateL Week
1 '2022-04-01' 1
2 '2022-04-02' 1
3 '2022-04-12' 2
4 '2022-04-27' 4
5 '2022-05-01' 0
其中 0 不属于@startdate 和@enddate 之间的范围。
如能向正确方向踢球,我们将不胜感激!谢谢!
试试这个代码:
SELECT job,dateL,
(
SELECT CASE
WHEN dateL BETWEEN @startdate AND @enddate THEN
(SELECT MAX(v) FROM (VALUES (1), (DATEDIFF(wk, @startdate, dateL))) AS VALUE(v))
ELSE 0
END
)
AS Week
FROM #test;