unr*_*boy 0 sql t-sql sql-server
数据间隔为15分钟:
Time Value 2010-01-01 00:15 3 2010-01-01 00:30 2 2010-01-01 00:45 4 2010-01-01 01:00 5 2010-01-01 01:15 1 2010-01-01 01:30 3 2010-01-01 01:45 4 2010-01-01 02:00 12 2010-01-01 02:15 13 2010-01-01 02:30 12 2010-01-01 02:45 14 2010-01-01 03:00 15 2010-01-01 03:15 3 2010-01-01 03:30 2 2010-01-01 03:45 3 2010-01-01 04:00 5 .......... .......... .......... 2010-01-02 00:00
通常会有96分.
根据这些值,我们可能会注意到从00:15到01:45的值彼此接近,从02:00到03:00它们彼此接近,从03:15到04:00他们彼此接近.
基于"彼此接近"的规则,我希望将数据"分组"为3个部分:
请考虑数据可以是随机的,并且可以根据上面定义的规则分组为3个以上,但最大值不应超过10个.并且分组必须遵守时间顺序,例如,您不能将00:15/02:30/04:45放入1组,因为这3个点不是连续的.
请介绍一下如何在t-sql中实现它.
更新: 值可能是:
Time Value 2010-01-01 00:15 3 2010-01-01 00:30 2 2010-01-01 00:45 4 2010-01-01 01:00 5 2010-01-01 01:15 1 2010-01-01 01:30 3 2010-01-01 01:45 4 2010-01-01 02:00 12 2010-01-01 02:15 13 2010-01-01 02:30 4 --suddenly decreased 2010-01-01 02:45 14 2010-01-01 03:00 15 2010-01-01 03:15 3 2010-01-01 03:30 2 2010-01-01 03:45 3 2010-01-01 04:00 5 .......... .......... .......... 2010-01-02 00:00
对于这种情况,我们不应该单独分组02:30,因为我们希望组大小必须至少为3分,我们将把这一点(02:30)放到上一组(从02:00到02:00) 03:00).
声明并填充testdata:
set nocount on
declare @result table(mintime datetime, maxtime datetime)
declare @t table(time datetime, value int)
-- variation is how much difference will be allowed from one row to the next
declare @variation int
set @variation = 5
insert @t values('2010-01-01 00:15',3)
insert @t values('2010-01-01 00:30',2)
insert @t values('2010-01-01 00:45',4)
insert @t values('2010-01-01 01:00',5)
insert @t values('2010-01-01 01:15',1)
insert @t values('2010-01-01 01:30',3)
insert @t values('2010-01-01 01:45',4)
insert @t values('2010-01-01 02:00',12)
insert @t values('2010-01-01 02:15',13)
insert @t values('2010-01-01 02:30',12)
insert @t values('2010-01-01 02:45',14)
insert @t values('2010-01-01 03:00',15)
insert @t values('2010-01-01 03:15',3)
insert @t values('2010-01-01 03:30',2)
insert @t values('2010-01-01 03:45',3)
insert @t values('2010-01-01 04:00',5)
Run Code Online (Sandbox Code Playgroud)
码:
a:
;with t as
( -- add a rownumber
select *, rn = row_number() over(order by time) from @t
), a as
(-- increase group if current row's value varies more than @variation from last row's value
select time, value, rn, 0 grp from t where rn = 1
union all
select t.time, t.value, t.rn, case when t.value between
a.value - @variation and a.value +@variation
then grp else grp+1 end
from t join a on
t.rn = a.rn +1
)
insert @result
select min(time), max(time) from a group by grp
if @@rowcount > 10
begin
-- this will activate if more than 10 groups of numbers are found
-- start over with higher tolerance for variation
set @variation=@variation + 1
delete @result
goto a
end
select convert(char(5), mintime,114) + ' to ' + convert(char(5), maxtime,114)
from @result
Run Code Online (Sandbox Code Playgroud)
结果如下:http: //data.stackexchange.com/stackoverflow/q/110891/declare-and-populate-testdata