Sql将3行5分钟分成15分钟的单个一行

时间:2016-07-29 14:18:02

标签: sql sql-server group-by intervals

我想将3行5分钟组合成15分钟的单行,如果需要,可以对3行进行更新。

以下是一个例子:

2016-07-29 14:00:00.000     2016-07-29 14:05:00.000
2016-07-29 14:05:00.000     2016-07-29 14:10:00.000
2016-07-29 14:10:00.000     2016-07-29 14:15:00.000
2016-07-29 14:15:00.000     2016-07-29 14:20:00.000
2016-07-29 14:20:00.000     2016-07-29 14:25:00.000
2016-07-29 14:25:00.000     2016-07-29 14:30:00.000
2016-07-29 14:30:00.000     2016-07-29 14:35:00.000
2016-07-29 14:35:00.000     2016-07-29 14:40:00.000
2016-07-29 14:40:00.000     2016-07-29 14:45:00.000
2016-07-29 14:45:00.000     2016-07-29 14:50:00.000
2016-07-29 14:50:00.000     2016-07-29 14:55:00.000
2016-07-29 14:55:00.000     2016-07-29 15:00:00.000
2016-07-29 15:00:00.000     2016-07-29 15:05:00.000

结果应该是:

2016-07-29 14:00:00.000     2016-07-29 14:15:00.000
2016-07-29 14:15:00.000     2016-07-29 14:30:00.000
2016-07-29 14:30:00.000     2016-07-29 14:45:00.000
2016-07-29 14:45:00.000     2016-07-29 15:00:00.000
...

谢谢! :)

3 个答案:

答案 0 :(得分:0)

基本上,您希望每次间隔15分钟,并将其用于聚合。像这样:

select distinct dateadd(minute, datediff(minute, 0, col1) / 15 * 15, 0),
       dateadd(minute, datediff(minute, 0, col1) / 15 * 15 + 15, 0)

答案 1 :(得分:0)

我使用UDF创建动态日期范围。您也可以使用数字/计数表

Declare @Date1 DateTime = '2016-07-29 14:00:00.000'
Declare @Date2 DateTime = '2016-07-30 00:00:00.000'

Select DateR1=RetVal,DateR2=DateAdd(MI,15,RetVal) from [dbo].[udf-Create-Range-Date](@Date1,@Date2,'MI',15) Where RetVal<@Date2

返回

DateR1                  DateR2
2016-07-29 14:00:00.000 2016-07-29 14:15:00.000
2016-07-29 14:15:00.000 2016-07-29 14:30:00.000
2016-07-29 14:30:00.000 2016-07-29 14:45:00.000
2016-07-29 14:45:00.000 2016-07-29 15:00:00.000
2016-07-29 15:00:00.000 2016-07-29 15:15:00.000
... 
2016-07-29 23:30:00.000 2016-07-29 23:45:00.000
2016-07-29 23:45:00.000 2016-07-30 00: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) 

答案 2 :(得分:0)

使用递归CTE ..

DEMO HERE

    ;With cte(sdate,edate,maxdate)
    as
    (
    select min(sdate),dateadd(minute,15,min(sdate) ),max(edate) as maxdate from #tt 
    union all
    select dateadd(minute,15,sdate),dateadd(minute,15,edate) ,maxdate
     from 
     cte
     where 
     edate<=maxdate
    )
   select sdate,edate from cte