[code=SQL]create table tb(id int,aid int,ipaddress varchar(20),time int)
insert into tb values(3016, 44151, '61.232.12.126' , 1190599009)
insert into tb values(3017, 44151, '61.232.12.126' , 1190599009)
insert into tb values(3018, 44151, '61.232.12.126' , 1190599010)
insert into tb values(3019, 44151, '61.232.12.126' , 1190599010)
insert into tb values(3020, 44151, '61.232.12.126' , 1190599010)
insert into tb values(3021, 44151, '61.232.12.126' , 1190599010)
insert into tb values(3022, 44151, '61.232.12.126' , 1190599010)
insert into tb values(3023, 46224, '61.133.210.146', 1190599011)
insert into tb values(3024, 45735, '218.11.201.203', 1190599014)
insert into tb values(3025, 44729, '218.82.108.38' , 1190599096)
insert into tb values(3026, 8627 , '61.181.245.85' , 1190599098)
insert into tb values(3027, 46226, '123.154.42.135', 1190599102)
insert into tb values(3028, 45111, '58.218.152.251', 1190599104)
go
select distinct m.ipaddress , isnull(sum(cnt),0) cnt from tb m left join
(
select t1.* , cnt = 1 from
(select px=(select count(1) from tb where ipaddress=a.ipaddress and id<a.id)+1 , * from tb a) t1,
(select px=(select count(1) from tb where ipaddress=a.ipaddress and id<a.id)+1 , * from tb a) t2
where t1.ipaddress = t2.ipaddress and t1.px = t2.px - 1 and t2.time - t1.time > 86400
) n
on m.ipaddress = n.ipaddress
group by m.ipaddress
drop table tb
/*
ipaddress cnt
-------------------- -----------
123.154.42.135 0
218.11.201.203 0
218.82.108.38 0
58.218.152.251 0
61.133.210.146 0
61.181.245.85 0
61.232.12.126 0
(所影响的行数为 7 行)
*/