SQL Server时间间隔为多行

时间:2017-08-04 10:22:30

标签: sql-server

我有StartDateTime和EndDateTime的这条记录,我希望结果是15分钟间隔的行:

示例数据:

FName | StartDateTime | EndDateTime :----- | -----------: | :--------------: Juan | 08/01/2017 1:00| 08/01/2017 8:00

结果:

FName | Interval :----- | -----------: Juan | 08/01/2017 1:00 Juan | 08/01/2017 1:15 Juan | 08/01/2017 1:30 Juan | 08/01/2017 1:45 Juan | 08/01/2017 2:00

在它到达EndDateTime之前,我怎样才能在SQL中实现这一点。我正在考虑使用循环语句或cte。

4 个答案:

答案 0 :(得分:0)

您可以使用recursive query这样的

DECLARE @SampleData AS TABLE
(
    FName varchar(20),
    StartDatetime datetime,
    EndDatetime datetime
)

INSERT INTO @SampleData
(
    FName,
    StartDatetime,
    EndDatetime
)
VALUES
('Juan', '2017-08-01 1:00', '2017-08-01 8:00')

;WITH temp AS
(
    SELECT sd.FName, sd.StartDatetime AS Interval, sd.EndDatetime
    FROM @SampleData sd

    UNION ALL

    SELECT t.FName, dateadd(minute, 15, t.Interval)  AS Interval, t.EndDatetime
    FROM temp t
    WHERE dateadd(minute, 15, t.Interval) <= t.EndDatetime
)
SELECT t.FName,
      t.Interval  
 FROM temp t
 OPTION (MAXRECURSION 0)

演示链接:http://rextester.com/CELP88345

答案 1 :(得分:0)

最好使用日期表,但您可以生成如下:

Select Fname, RowN as Interval from #dates
cross apply (
    Select top(datediff(hh,startdatetime, enddatetime)*4+1) RowN =dateadd(mi,  (row_number() over(order by (Select NULL)) -1)*15 ,startdatetime)
        from master..spt_values s1, master..spt_values s2 ) a

输出如下:


+-------+-------------------------+
| Fname |        Interval         |
+-------+-------------------------+
| Juan  | 2017-08-01 01:00:00.000 |
| Juan  | 2017-08-01 01:15:00.000 |
| Juan  | 2017-08-01 01:30:00.000 |
| Juan  | 2017-08-01 01:45:00.000 |
| Juan  | 2017-08-01 02:00:00.000 |
| Juan  | 2017-08-01 02:15:00.000 |
| Juan  | 2017-08-01 02:30:00.000 |
| Juan  | 2017-08-01 02:45:00.000 |
| Juan  | 2017-08-01 03:00:00.000 |
| Juan  | 2017-08-01 03:15:00.000 |
| Juan  | 2017-08-01 03:30:00.000 |
| Juan  | 2017-08-01 03:45:00.000 |
| Juan  | 2017-08-01 04:00:00.000 |
| Juan  | 2017-08-01 04:15:00.000 |
| Juan  | 2017-08-01 04:30:00.000 |
| Juan  | 2017-08-01 04:45:00.000 |
| Juan  | 2017-08-01 05:00:00.000 |
| Juan  | 2017-08-01 05:15:00.000 |
| Juan  | 2017-08-01 05:30:00.000 |
| Juan  | 2017-08-01 05:45:00.000 |
| Juan  | 2017-08-01 06:00:00.000 |
| Juan  | 2017-08-01 06:15:00.000 |
| Juan  | 2017-08-01 06:30:00.000 |
| Juan  | 2017-08-01 06:45:00.000 |
| Juan  | 2017-08-01 07:00:00.000 |
| Juan  | 2017-08-01 07:15:00.000 |
| Juan  | 2017-08-01 07:30:00.000 |
| Juan  | 2017-08-01 07:45:00.000 |
| Juan  | 2017-08-01 08:00:00.000 |
+-------+-------------------------+

答案 2 :(得分:0)

您可以使用递归CTE来达到预期效果。

演示 - http://sqlfiddle.com/#!6/821a5/11

with cte as 
(
   select Fname,StartDateTime
   from Table1
   union all
   select  Fname,DATEADD(mi,15,StartDateTime) as StartDateTime
   from cte
   where  DATEADD(mi,15,StartDateTime) <= '2017-01-08 08:00:00:000'
  )

 select Fname, StartDateTime  from cte; 

答案 3 :(得分:0)

如果您对TVF(表格值函数)开放

我经常使用此udf来创建动态日期/时间范围。它比递归CTE和参数驱动更快。

参数是Date1,Date2,DatePart和Increment

示例

Declare @YourTable Table ([FName] varchar(50),[StartDateTime] datetime,[EndDateTime] datetime)
Insert Into @YourTable Values 
 ('Juan','08/01/2017 1:00','08/01/2017 8:00')

Select A.FName
      ,B.* 
 From  @YourTable A
 Cross Apply [dbo].[udf-Range-Date](A.StartDateTime,A.EndDateTime,'MI',15) B

<强>返回

FName   RetSeq  RetVal
Juan    1       2017-08-01 01:00:00.000
Juan    2       2017-08-01 01:15:00.000
Juan    3       2017-08-01 01:30:00.000
Juan    4       2017-08-01 01:45:00.000
Juan    5       2017-08-01 02:00:00.000
...
Juan    29      2017-08-01 08:00:00.000

感兴趣的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) 
*/