一道MSSQL笔试题
有一组通话记录,数据量较大。信息表结构如下:
ID 主叫号码 被叫号码通话起始时间 通话结束时间 通话时长
其中ID为主键
求其中同一个号码的两次通话之间间隔大于某个时间的通话记录ID 。不妨设时间间隔为2分钟
一、
select distinct id
from tb t
where id in (select id from tb where [主叫号码]=t.[主叫号码] and (datediff(minute,t.[通话起始时间],[通话结束时间])>=2))
or id=(case when (select count(*) from tb where [主叫号码]=t.[主叫号码] and (datediff(minute,t.[通话起始时间],[通话结束时间])>=2))>0 then id else 0 end)
二、
select t.id as '首次主叫ID'
,b.id as '下次叫ID'
from tb t
inner join tb b
on t.[主叫号码]=b.[主叫号码]
and datediff(minute,t.[通话起始时间],b.[通话结束时间])>2
and t.id<>b.id
希望大家奉献更好的方法。共同学习,不胜感激。