我在
这样的表中有不同的字段Caller_id Called_number Date time Minutes Charges
在为客户提取账单时,有重叠的电话,如果您指导我如何避免重叠电话的sql脚本,我将不胜感激。
例如。在一个Caller_id处,似乎同时有两个呼叫,同一时间是不可能的。
CallerId Called_number Date Time Minutes Charges 5555555555 42555777777 9/12/2013 17:15:46 44 44 5555555555 5556666666 9/12/2013 17:21:28 5 9.25
提前致谢
答案 0 :(得分:1)
如果您确实要提取包含的调用,可以使用以下代码:
create table t (
Caller_id bigint,
Called_number bigint,
call_date datetime,
duration float,
charge float);
insert into t
values
(5555555555, 42555777777, '2013-12-09 17:15:46', 44, 44),
(5555555555, 5556666666, '2013-12-09 17:21:28', 5,9.25);
select t.*
from t join ( select caller_id, called_number, call_date as start_date,
date_add(call_date,INTERVAL duration minute) as end_date from t) t1 on (t.caller_id = t1.caller_id and t.call_date between t1.start_date and t1.end_date) where t.called_number != t1.called_number
你也可以看一下这篇文章: Overlapping Date Ranges - Identifying Only the Overlap