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

请问怎么查询出一个表里全部时间间隔小于100毫秒的记录?
例如A表,有字段ID,CreateDateTime


数据为
1 2012-02-03 10:29:04.812
2 2012-02-03 10:29:04.823
3 2012-02-03 10:29:04.943
4 2012-02-03 11:00:07.145
5 2012-02-03 11:00:07.452
6 2012-02-03 11:00:07.468
7 2012-02-03 11:00:07.523


需要得到的结果为
1 2012-02-03 10:29:04.812
2 2012-02-03 10:29:04.823
5 2012-02-03 11:00:07.452
6 2012-02-03 11:00:07.468
7 2012-02-03 11:00:07.523


想看看有多少操作是属于并发的,或者有没有其他的方法?谢谢!

------解决方案--------------------
上面有问题

SQL code
select * from a t
where exists (
  select 1 
  from a
  where abs(datediff(millisecond,CreateDateTime,t.CreateDateTime)) <= 100 
  and id <> t.id
)

------解决方案--------------------
SQL code
--例如A表,有字段ID,CreateDateTime


--数据为
--1 2012-02-03 10:29:04.812
--2 2012-02-03 10:29:04.823
--3 2012-02-03 10:29:04.943
--4 2012-02-03 11:00:07.145
--5 2012-02-03 11:00:07.452
--6 2012-02-03 11:00:07.468
--7 2012-02-03 11:00:07.523

if OBJECT_ID('tb')is not null 
drop table tb
go 
create table tb (ID int,CreateDateTime varchar(50))
insert into tb 
select 1, '2012-02-03 10:29:04.812' union all
select 2, '2012-02-03 10:29:04.823' union all
select 3, '2012-02-03 10:29:04.943' union all
select 4, '2012-02-03 11:00:07.145' union all
select 5, '2012-02-03 11:00:07.452' union all
select 6, '2012-02-03 11:00:07.468' union all
select 7, '2012-02-03 11:00:07.523'  

select * from tb t where exists (select * from tb 
where abs(datediff(millisecond,CreateDateTime,t.CreateDateTime)) <= 100 and id <> t.id
)





ID          CreateDateTime
----------- --------------------------------------------------
1           2012-02-03 10:29:04.812
2           2012-02-03 10:29:04.823
5           2012-02-03 11:00:07.452
6           2012-02-03 11:00:07.468
7           2012-02-03 11:00:07.523

(5 行受影响)