根据sql server中的相关值进行数据分组

时间:2011-08-30 14:13:49

标签: sql sql-server tsql

数据间隔为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个部分:

  • 00:15至01:45
  • 02:00至03:00
  • 03:15至04:00

请注意,数据可以是随机的,并且可以根据上面定义的规则分组为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至03:00)。

2 个答案:

答案 0 :(得分:7)

声明并填充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)

代码:

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

结果在这里: http://data.stackexchange.com/stackoverflow/q/110891/declare-and-populate-testdata

答案 1 :(得分:0)

由于您的问题发生了很大变化,这里是新问题的新答案,我只包含了代码部分。

declare @t table(time datetime, value int)
declare @variation float
set @variation = 2
set nocount on

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',52)
insert @t values('2010-01-01 02:15',5)
insert @t values('2010-01-01 02:30',52)
insert @t values('2010-01-01 02:45',54)
insert @t values('2010-01-01 03:00',55)
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)


declare @result table(mintime datetime, maxtime datetime)
a:
delete @result

;with t as
(
select *, rn = row_number() over(order by time), log(value) lv from @t where datediff(day, time, '2010-01-01') = 0
), a as
(
select time, lv, rn, 0 grp from t where rn = 1
union all
select t1.time, a.lv, t1.rn, 
case when exists (select 1 from t t2 where t1.rn between rn + 1 and rn + 3 and 
lv between t1.lv - @variation and t1.lv +@variation) then grp else grp + 1 end
from t t1 join a on 
t1.rn = a.rn +1
)
insert @result
select min(time), max(time) from a group by grp

if @@rowcount > 10 
begin 
    set @variation=@variation + .5 
    goto a 
end

select * from @result

结果:

mintime                     maxtime
2010-01-01 00:15:00.000     2010-01-01 01:45:00.000
2010-01-01 02:00:00.000     2010-01-01 03:00:00.000
2010-01-01 03:15:00.000     2010-01-01 04:00:00.000