gpt4 book ai didi

sql-server - SQL Server - 生成重复日期的计划列表

转载 作者:行者123 更新时间:2023-12-04 17:00:01 25 4
gpt4 key购买 nike

我们有一组记录,其中每条记录(工作)都有一个以天为单位的周期(或频率),用于确定工作何时出现在某人的工作列表中。 E.G

作业 A 每 7 天生成一次作业 B 每 14 天生成一次。

每个工作还有一个开始日期。

我需要生成一个表格,其中包含一个日期范围内所有可能的工作日期。

这是我生成可能日期的代码的第一部分。

DECLARE @ForecastEarliestStartDate as DATETIME, @formStartDate as DATE,@formEndDate as DATE, @cycleInDays as BIGINT;

--set input param values
SET @ForecastEarliestStartDate = CAST('2017-07-03' AS DATETIME)
SET @formStartDate = getdate();
SET @formEndDate = getdate()+60;
SET @cycleInDays = 28;


WITH mycte AS
(
SELECT @ForecastEarliestStartDate DateValue
UNION ALL
SELECT DateValue + @cycleInDays
FROM mycte
WHERE DateValue + @cycleInDays < @formEndDate
)

SELECT *
FROM mycte
WHERE DateValue between @formStartDate and @formEndDate
OPTION (MAXRECURSION 0);

这对于单个作业来说效果很好,但是我如何针对整个作业表运行它,其中 @ForecastEarliestStartDate、@formStartDate、@formEndDate、@cycleInDays 是表中的字段?

最佳答案

我经常使用 TVF 来创建动态日期/时间范围。通常比递归 CTE 更快,并且它是参数驱动的。您提供日期/时间范围、日期部分和增量。

例如

Select * From [dbo].[udf-Range-Date]('2017-07-03',getdate()+60,'DD',7)

返回

RetSeq  RetVal
1 2017-07-03
2 2017-07-10
3 2017-07-17
4 2017-07-24
5 2017-07-31
6 2017-08-07
7 2017-08-14
8 2017-08-21
9 2017-08-28

UDF(如果有兴趣)

CREATE FUNCTION [dbo].[udf-Range-Date] (@R1 datetime,@R2 datetime,@Part varchar(10),@Incr int)
Returns Table
Return (
with cte0(M) As (Select 1+Case @Part When 'YY' then DateDiff(YY,@R1,@R2)/@Incr When 'QQ' then DateDiff(QQ,@R1,@R2)/@Incr When 'MM' then DateDiff(MM,@R1,@R2)/@Incr When 'WK' then DateDiff(WK,@R1,@R2)/@Incr When 'DD' then DateDiff(DD,@R1,@R2)/@Incr When 'HH' then DateDiff(HH,@R1,@R2)/@Incr When 'MI' then DateDiff(MI,@R1,@R2)/@Incr When 'SS' then DateDiff(SS,@R1,@R2)/@Incr End),
cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (Select M from cte0) Row_Number() over (Order By (Select NULL)) From cte1 a, cte1 b, cte1 c, cte1 d, cte1 e, cte1 f, cte1 g, cte1 h ),
cte3(N,D) As (Select 0,@R1 Union All Select N,Case @Part When 'YY' then DateAdd(YY, N*@Incr, @R1) When 'QQ' then DateAdd(QQ, N*@Incr, @R1) When 'MM' then DateAdd(MM, N*@Incr, @R1) When 'WK' then DateAdd(WK, N*@Incr, @R1) When 'DD' then DateAdd(DD, N*@Incr, @R1) When 'HH' then DateAdd(HH, N*@Incr, @R1) When 'MI' then DateAdd(MI, N*@Incr, @R1) When 'SS' then DateAdd(SS, N*@Incr, @R1) End From cte2 )

Select RetSeq = N+1
,RetVal = D
From cte3,cte0
Where D<=@R2
)
/*
Max 100 million observations -- Date Parts YY QQ MM WK DD HH MI SS
Syntax:
Select * from [dbo].[udf-Range-Date]('2016-10-01','2020-10-01','YY',1)
Select * from [dbo].[udf-Range-Date]('2016-01-01','2017-01-01','MM',1)
*/

Edit - Working Example of CROSS APPLY

Declare @YourTable table (JobName varchar(50),StartDate date,Interval int)
Insert Into @YourTable values
('Job A','2017-07-03',7)
,('Job B','2017-07-03',14)


Select A.JobName
,JobDate = B.RetVal
From @YourTable A
Cross Apply [dbo].[udf-Range-Date](A.StartDate,getdate()+60,'DD',A.Interval) B

返回

JobName JobDate
Job A 2017-07-03
Job A 2017-07-10
Job A 2017-07-17
Job A 2017-07-24
Job A 2017-07-31
Job A 2017-08-07
Job A 2017-08-14
Job A 2017-08-21
Job A 2017-08-28
Job B 2017-07-03 -- << Notice differant span for Job B
Job B 2017-07-17
Job B 2017-07-31
Job B 2017-08-14
Job B 2017-08-28

关于sql-server - SQL Server - 生成重复日期的计划列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44904090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com