我想要实现的是在某种时间轴上对发生的事件进行计数,将重叠事件视为单个事件,从这样的字段开始并使用TSQL:
Pattern (JSON array of couple of values indicating
the start day and the duration of the event)
----------------------------------------------------
[[0,22],[0,24],[18,10],[30,3]]
----------------------------------------------------
对于此示例,预期结果应为30
我需要的是一个TSQL函数来获取此号码...
即使我不确定这是正确的路径,我仍在尝试在数据集的行之间模拟某种BINARY OR。经过一番尝试后,我设法将我的数据集变成了这样的东西:
start | length | pattern
----------------------------------------------------
0 | 22 | 1111111111111111111111
0 | 24 | 111111111111111111111111
18 | 10 | 000000000000000001111111111
30 | 3 | 000000000000000000000000000000111
----------------------------------------------------
但是现在我不知道如何在TSQL中进行=)一种解决方案,因为我说过可以在“模式”字段之间进行二进制或以获取类似信息:
1111111111111111111111...........
111111111111111111111111.........
000000000000000001111111111......
000000000000000000000000000000111
--------------------------------------
111111111111111111111111111000111
是否可以在TSQL中做到这一点?
也许我只是在这里使事情复杂化,您还有其他想法吗?
不要忘记我只需要结果编号!!!
谢谢你们
根据您输入的日期,您应该可以执行以下操作来计算发生事件的天数。
所述cte
用于产生的日期,其中由两个日期变量定义的开始和结束的一个表。这些将最适合作为源数据驱动的数据。如果必须使用带编号的日期值,则可以简单地返回递增的数字而不是递增的日期:
declare @Events table (StartDate date
,DaysLength int
)
insert into @Events values
('20160801',22)
,('20160801',24)
,('20160818',10)
,('20160830',3)
declare @StartDate date = getdate()-30
,@EndDate date = getdate()+30
;with Dates As
(
select DATEADD(day,1,@StartDate) as Dates
union all
select DATEADD(day,1, Dates)
from Dates
where Dates < @EndDate
)
select count(distinct d.Dates) as EventingDays
from Dates d
inner join @Events e
on(d.Dates between e.StartDate and dateadd(d,e.DaysLength-1,e.StartDate)
)
option(maxrecursion 0)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句