一组通话记录(总共500万条):
ID 主叫号码 被叫号码通话起始时间 通话结束时间 通话时长
1 98290000 0215466546656 2007-02-01 09:49:53.000 2007-02-01 09:50:16.000 23
2 98290000 021546654666 2007-02-01 09:50:29.000 2007-02-01 09:50:41.000 12
3 98290000 021546654666 2007-02-01 09:50:58.000 2007-02-01 09:51:12.000 14
4 68290900 0755133329866 2007-02-01 10:04:31.000 2007-02-01 10:07:13.000 162
5 78290000 0755255708638 2007-02-01 10:48:26.000 2007-02-01 10:49:23.000 57
6 78290000 0755821119109 2007-02-01 10:49:39.000 2007-02-01 10:52:55.000 196
7 78290000 035730928370 2007-02-01 11:30:45.000 2007-02-01 11:31:58.000 73
8 78290000 0871138889904 2007-02-01 11:33:47.000 2007-02-01 11:35:00.000 73
9 68290000 035730928379 2007-02-01 11:52:20.000 2007-02-01 11:54:56.000 156
10 68290000 0298521811199 2007-02-01 12:44:45.000 2007-02-01 12:45:04.000 19
求其中同一个号码的两次通话之间间隔大于10秒的通话记录ID
例如:6,7,8,9,10条记录均符合。
在查询分析器下的代码
create table tbl
(
ID int ,
主叫号码 varchar(15),
被叫号码 varchar(15),
通话起始时间 datetime,
通话结束时间 datetime ,
通话时长 int ,
)
truncate table tbl
insert into tbl
select 1 , '98290000', '0215466546656', '2007-02-01 09:49:53.000', '2007-02-01 09:50:16.000', 23
union all select 2, '98290000', '021546654666', '2007-02-01 09:50:29.000', '2007-02-01 09:50:41.000', 12
union all select 3, '98290000', '021546654666', '2007-02-01 09:50:58.000', '2007-02-01 09:51:12.000', 14
union all select 4, '68290000', '0755133329866', '2007-02-01 10:04:31.000', '2007-02-01 10:07:13.000', 162
union all select 5, '78290000', '0755255708638', '2007-02-01 10:48:26.000', '2007-02-01 10:49:23.000', 57
union all select 6, '78290000', '0755821119109', '2007-02-01 10:49:39.000', '2007-02-01 10:52:55.000', 196
union all select 7, '78290000', '035730928370', '2007-02-01 11:30:45.000', '2007-02-01 11:31:58.000', 73
union all select 8, '78290000', '0871138889904', '2007-02-01 11:33:47.000', '2007-02-01 11:35:00.000', 73
union all select 9, '68290000', '035730928379', '2007-02-01 11:52:20.000', '2007-02-01 11:54:56.000', 156
union all select 10, '68290000', '0298521811199', '2007-02-01 12:44:45.000', '2007-02-01 12:45:04.000', 19
然后就是查询
select *
from tbl t
where exists
(
select 1
from
(
select top 1 *
from tbl a
where exists(select 1 from tbl where a.主叫号码=主叫号码 and a.id <id and t.id =id)
order by ID desc
)m
where t.主叫号码 =m.主叫号码 and datediff(n ,m.通话结束时间,t.通话起始时间)>10--大于10分钟的
)