【问题标题】:sql get row interval of 5 minute between two datesql获取两个日期之间5分钟的行间隔
【发布时间】:2016-07-27 17:18:19
【问题描述】:

有没有办法在日期之间获得像 5,10 或 15 分钟这样的分钟间隔?这是一个例子:

我有两个约会:

2016-07-25 14:00:00.000 || 2016-07-25 18:00:00.000

我想要一个函数,它可以根据我给出的参数间隔返回一组行:

当间隔参数为5分钟时:

2016-07-25 14:05:00.000
2016-07-25 14:10:00.000
2016-07-25 14:15:00.000
2016-07-25 14:20:00.000
2016-07-25 14:25:00.000
2016-07-25 ...
2016-07-25 18:00:00.000

当间隔参数为10分钟时:

2016-07-25 14:10:00.000
2016-07-25 14:20:00.000
2016-07-25 14:30:00.000
2016-07-25 14:40:00.000
2016-07-25 14:50:00.000
2016-07-25 ...
2016-07-25 18:00:00.000

谢谢!

【问题讨论】:

  • 是的,有办法。您需要一个像 time dimension table 这样的表格,其中包含您感兴趣的区间。
  • 用您正在使用的数据库标记您的问题。

标签: sql database-administration schedule


【解决方案1】:

Demo Here

使用递归 CTE,这可以插入到一个函数中..

declare @start datetime='2016-07-25 14:00:00.000'
declare @end datetime='2016-07-25 18:00:00.000'

;With cte(starttime,endtime)
as
(
select @start,@end
union all
select dateadd(minute,5,starttime),endtime
from cte c
where datediff(minute,c.starttime,c.endtime)>0
)
select * from cte

【讨论】:

    【解决方案2】:

    您可以使用 Numbers/Tally 表,但我使用 UDF 创建动态日期范围

    Select * from [dbo].[udf-Create-Range-Date]('2016-07-25 14:00:00.000','2016-07-25 18:00:00.000','MI',5)
    

    注意不仅是start/end参数,还有DatePart和Increment

    返回

    RetVal
    2016-07-25 14:00:00.000
    2016-07-25 14:05:00.000
    2016-07-25 14:10:00.000
    ...
    2016-07-25 17:55:00.000
    2016-07-25 18:00:00.000
    

    UDF

    CREATE FUNCTION [dbo].[udf-Create-Range-Date] (@DateFrom datetime,@DateTo datetime,@DatePart varchar(10),@Incr int)
    
    Returns 
    @ReturnVal Table (RetVal datetime)
    
    As
    Begin
        With DateTable As (
            Select DateFrom = @DateFrom
            Union All
            Select Case @DatePart
                   When 'YY' then DateAdd(YY, @Incr, df.dateFrom)
                   When 'QQ' then DateAdd(QQ, @Incr, df.dateFrom)
                   When 'MM' then DateAdd(MM, @Incr, df.dateFrom)
                   When 'WK' then DateAdd(WK, @Incr, df.dateFrom)
                   When 'DD' then DateAdd(DD, @Incr, df.dateFrom)
                   When 'HH' then DateAdd(HH, @Incr, df.dateFrom)
                   When 'MI' then DateAdd(MI, @Incr, df.dateFrom)
                   When 'SS' then DateAdd(SS, @Incr, df.dateFrom)
                   End
            From DateTable DF
            Where DF.DateFrom < @DateTo
        )
    
        Insert into @ReturnVal(RetVal) Select DateFrom From DateTable option (maxrecursion 32767)
    
        Return
    End
    
    -- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2020-10-01','YY',1) 
    -- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2020-10-01','DD',1) 
    -- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2016-10-31','MI',15) 
    -- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2016-10-02','SS',1) 
    

    【讨论】:

      【解决方案3】:

      基本上,我认为您正在寻找的是这个脚本:

      CREATE FUNCTION dbo.udf1 (@interval int, @start datetime, @end datetime)
      RETURNS @out TABLE (date1 datetime)
      BEGIN
      --declare @start datetime='2016-07-25 14:00:00.000', @end datetime='2016-07-25 18:00:00.000', @curr datetime
      
      set @curr = @start
      while (dateadd(mi,@interval,@curr)<=@end)
      begin
          set @curr = dateadd(mi,@interval,@curr)
          insert into @out
          select @curr
      end
      return;
      END
      
      select *
      from dbo.udf1(10,'2016-07-25 14:00:00.000','2016-07-25 18:00:00.000')
      

      【讨论】:

      • 好剧本。我建议 let start , end 作为参数
      【解决方案4】:

      感谢您的回答,我知道如何使用它

       create function fn_daterange
         (
         @MinDate as datetime,
         @MaxDate as datetime,
         @intval  as datetime
         )
      returns table
      --**************************************************************************
      -- Procedure: fn_daterange()
      --    Author: Ron Savage
      --      Date: 12/16/2008
      --
      -- Description:
      -- This function takes a starting and ending date and an interval, then
      -- returns a table of all the dates in that range at the specified interval.
      --
      -- Change History:
      -- Date        Init. Description
      -- 12/16/2008  RS    Created.
      -- **************************************************************************
      as
      return
         WITH times (startdate, enddate, intervl) AS
            (
            SELECT @MinDate as startdate, @MinDate + @intval as enddate, @intval as intervl
               UNION ALL
            SELECT startdate + intervl as startdate, enddate + intervl as enddate, intervl as intervl
            FROM times
            WHERE startdate + intervl <= @MaxDate
            )
         select startdate, enddate from times;
      
      go
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-23
        • 1970-01-01
        • 1970-01-01
        • 2019-08-01
        • 2017-11-05
        • 1970-01-01
        相关资源
        最近更新 更多