34,873
社区成员
发帖
与我相关
我的任务
分享
create table #Tb
(ID int,
主叫号码 nvarchar(20),
被叫号码 nvarchar(20),
通话起始时间 datetime,
通话结束时间 datetime,
通话时长 int)
insert #Tb
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,'68290900','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
;with TempA as(select Row_Number()over(partition by 主叫号码 order by 通话起始时间) as num,* from #Tb)
,TempB as (select t1.id as id1,t2.id as id2
from TempA as t1 join TempA as t2 on t1.主叫号码=t2.主叫号码 and t1.num=t2.num-1
where datediff(mi,t1.通话结束时间,t2.通话起始时间)>=10)
select id1 from TempB
union
select id2 from TempB
declare @tbl table
(
ID int ,
callNumber varchar(15),
RecNumber varchar(15),
starttime datetime,
endtime datetime ,
timecount int
)
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
--因全部记录相邻间隔都>10s,所以改为20s
declare @s int
set @s=20
select * from @tbl a
where not exists(select * from
(select top 1 * from @tbl where callNumber=a.callnumber and starttime>a.endtime order by starttime) b
where datediff(ss,a.endtime,starttime)<@s )
and not exists(select * from
(select top 1 * from @tbl where callNumber=a.callnumber and endtime<a.starttime order by endtime desc) b
where datediff(ss,endtime,a.starttime)<@s)
order by id