日期:2014-05-18 浏览次数:20710 次
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?