日期:2014-05-18  浏览次数:20670 次

关于表内记录间比较统计分析的问题请教
一个表TA:
ID - TIME - VAL1 - VAL2 - VAL3 - VAL4
-------------------------------------

内有数据若干行,现在想对表内的数据做一个记录间的比较分析。
比较的要求如下:
ID相同的记录成组,并且按照时间排序,组内进行比较,
相邻记录TIME时间戳的差值小于一个规定的值delta time,并且
VAL1=VAL3 AND VAL2=VAL4,如果相邻记录满足条件则标记一个事件
输出。

这样的一个需求,请问如何设计实现方法?需要使用游标进行全表遍历么?
我没有好的思路。

------解决方案--------------------
最好放點測數據,並貼上想要的結果,這樣做起來會快一些
------解决方案--------------------
自己写个函数
------解决方案--------------------
SQL code
create table tb(ID varchar(10),[TIME] varchar(20),VAL1 int,VAL2 int,VAL3 int,VAL4 int,EVENT int)
insert into tb select '001','1306387859417',34083,49251,34083,49252,0
insert into tb select '001','1306387894989',34083,49252,34083,49251,0
insert into tb select '001','1306387914068',34083,49251,34083,49252,0
insert into tb select '001','1306387931561',34083,49252,34083,49251,0
insert into tb select '001','1306388065622',34083,49251,34083,49252,0
insert into tb select '001','1306388083331',34083,49252,34083,49251,0
insert into tb select '001','1306388786316',34083,49251,34083,49252,0
insert into tb select '001','1306388793342',34083,49252,34083,49251,0
insert into tb select '001','1306388971073',34083,49251,34083,49252,0
insert into tb select '001','1306388972345',34083,49257,34083,49258,0 --插入一个不符合条件的以示区别
insert into tb select '001','1306388999223',34083,49252,34083,49251,0
insert into tb select '001','1306389126523',34083,49251,34083,49252,0
go
;with c1 as(
select id,[time],val1,val2,val3,val4,row_number()over(partition by id,val1 order by time)cn from tb
),c2 as(
select * from c1 a where exists(select 1 from c1 where id=a.id and cn=a.cn+1 and val1=a.val1 and val2=a.val4 and val4=a.val2)
union all
select * from c1 a where exists(select 1 from c1 where id=a.id and cn=a.cn-1 and val1=a.val1 and val2=a.val4 and val4=a.val2)
)update tb set [event]=1 where [time] in (select distinct [time] from c2)
select * from tb
/*
ID         TIME                 VAL1        VAL2        VAL3        VAL4        EVENT
---------- -------------------- ----------- ----------- ----------- ----------- -----------
001        1306387859417        34083       49251       34083       49252       1
001        1306387894989        34083       49252       34083       49251       1
001        1306387914068        34083       49251       34083       49252       1
001        1306387931561        34083       49252       34083       49251       1
001        1306388065622        34083       49251       34083       49252       1
001        1306388083331        34083       49252       34083       49251       1
001        1306388786316        34083       49251       34083       49252       1
001        1306388793342        34083       49252       34083       49251       1
001        1306388971073        34083       49251       34083       49252       1
001        1306388972345        34083       49257       34083       49258       0
001        1306388999223        34083       49252       34083       49251       1
001        1306389126523        34083       49251       34083       49252       1

(12 行受影响)
*/
go
drop table tb

------解决方案--------------------
按照你提供的测试数据,结果是什么

是不是只有 001 1306389126523 34083 49251 34083 49252 这一行的event标识为1?