SQL重复日期列表,包含开始日期,结束日期,关闭日期和关闭间隔

时间:2019-04-06 15:30:59

标签: sql-server-2012

在给定以下数据的情况下试图填充表(SQL Server):开始和结束日期(date),休息日(varchar),重复间隔的频率(int)

StartData-04/1/2018,EndData-04/30/2018,offdays ='Thrusday',freq = 1

输出:

_ONE_DAY_IN_SECONDS = 60 * 60 * 24


class RouterAgent(controller_agent_pb2_grpc.AgentServiceServicer):

    def EchoMessage(self, request, context):
        print("Echo : %s" %request.message)
        return controller_agent_pb2.EchoReply(message="Echo : %s" % request.message)


def serve():
    server = grpc.server(ThreadPoolExecutor(max_workers=10))
    controller_agent_pb2_grpc.add_AgentServiceServicer_to_server(RouterAgent(), server)
    server.add_insecure_port('[::]:50051')
    server.start()
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)

1 个答案:

答案 0 :(得分:1)

如果打开表值(帮助器)函数,请考虑以下内容

示例

Declare @Date1 date = '2018-04-01'
Declare @Date2 date = '2018-04-30'
Declare @Freq  int  =  1
Declare @OffDays varchar(50) = 'Thursday'  -- Try 'Saturday,Sunday'


Select * 
 From  [dbo].[tvf-Range-Date](@Date1,@Date2,'DD',@Freq) 
 Where charindex(DateName(WEEKDAY,RetVal),@OffDays)=0

返回

enter image description here

感兴趣的功能

CREATE FUNCTION [dbo].[tvf-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].[tvf-Range-Date]('2016-10-01','2020-10-01','YY',1) 
Select * from [dbo].[tvf-Range-Date]('2016-01-01','2017-01-01','MM',1) 
*/