我有一个看起来像下面的表,我试图通过持续时间列将其拆分,所以我得到,在下面的例子中,60行,但每分钟时间间隔,意味着时间将增加一分钟。
Filmtitle startTime duration
----------------------------------------------------------------
WW2's most daring raids 2016-10-03 00:00:00.000 60
期望的结果:
WW2's most daring raids 2016-10-03 00:00:01.000 60
WW2's most daring raids 2016-10-03 00:00:02.000 60
...
WW2's most daring raids 2016-10-03 00:00:59.000 60
有什么想法吗?
提前致谢。
答案 0 :(得分:0)
我有一个可能有帮助的TVF。它用于创建动态日期范围。 Tally / Numbers表也可以解决这个问题。
Filmtitle SomeName duration
WW2's most daring raids 2016-10-03 00:00:00.000 60
WW2's most daring raids 2016-10-03 00:01:00.000 60
WW2's most daring raids 2016-10-03 00:02:00.000 60
---
WW2's most daring raids 2016-10-03 00:57:00.000 60
WW2's most daring raids 2016-10-03 00:58:00.000 60
WW2's most daring raids 2016-10-03 00:59:00.000 60
返回
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)
*/
需要的功能
$car->type()
答案 1 :(得分:0)
如果您不想要UDF,则另一个选项是ad-hoc计数表
Declare @YourTable table (Filmtitle varchar(100),startTime datetime,duration int)
Insert Into @YourTable values
('WW2''s most daring raids','2016-10-03 00:00:00.000',60)
Select A.Filmtitle
,SomeName=DateAdd(MI,RN,startTime)
,A.duration
From @YourTable A
Join (
Select RN=Row_Number() over (Order By (Select Null))
From (Select N=1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)) A
Join (Select N=1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)) B on 1=1
) B on 1=1
Where B.RN<A.duration
返回
Filmtitle SomeName duration
WW2's most daring raids 2016-10-03 00:00:00.000 60
WW2's most daring raids 2016-10-03 00:01:00.000 60
WW2's most daring raids 2016-10-03 00:02:00.000 60
---
WW2's most daring raids 2016-10-03 00:57:00.000 60
WW2's most daring raids 2016-10-03 00:58:00.000 60
WW2's most daring raids 2016-10-03 00:59:00.000 60
答案 2 :(得分:0)
您也可以试试这个。我们的想法是将持续时间转换为从1开始到等于-1的等效行数。然后将row_number添加到startdate。
declare @table table(Film varchar(50), StartDate datetime, duration int)
insert into @table values
('WW2''s most daring raids', '2016-10-03 00:00:00.000', 60)
;with cte as(
select Film, StartDate, cast(Replicate('<k>1</k>', Duration-1) as xml) as x
from @table
)
, cte2 as(
select Film, startdate,
ROW_NUMBER() over(partition by Film, StartDate order by (select null)) as RN
from cte
outer apply x.nodes('/k') t(Dur)
)
select Film, Startdate, dateadd(second, RN, StartDate) as EndDate
from cte2 as a