I have a table called 'calls', columns are:

a_imei_number (imei of caller)
b_imei_number (imei of number which is called)
a_phone_number (number of caller)
b_phone_number (number of number which is called)
call_start_time (as hour)
call_end_time (as hour)
call_day (as day)

I am trying to find calls whic has cloned Imei numbers so at intersected time there must be at least 2 row so phone_numbers must be different and imei_numbers are same. like

id   a_imei  b_imei  a_phone   b_phone   call_start_time  call_end_time  call_day 
1    A       b       123       321        13:30            13:50          01.02.2014
2    A       c       999       888        13:41            13:55          01.02.2014

how can i join that table in its own ?

note: a_imei of a row may be same with b_imei of other row, because maybe one cloned phone is calling and clone of that phone is called by someone.

有帮助吗?

解决方案

Here's how to get all of the a_imei clones:

SELECT  c1.*
FROM    calls As c1
JOIN    calls As c2
    ON  c1.a_imei           =   c2.a_imei
    AND c1.a_phone          !=  c2.a_phone
    AND c1.call_start_time  <   c2.call_end_time
    AND c1.call_end_time    >   c2.call_start_time
ORDER BY c1.a_imei, c1.call_start_time, c1.call_end_time

Here's how to compare both the "a"s and "b"s together:

SELECT  c1.*, c2.*
FROM    calls As c1
JOIN    calls As c2
    ON  c1.call_start_time < c2.call_end_time
    AND c1.call_end_time   > c2.call_start_time
    AND (   (c1.a_imei     = c2.a_imei AND c1.a_phone != c2.a_phone)
        OR  (c1.a_imei     = c2.b_imei AND c1.a_phone != c2.b_phone)
        OR  (c1.b_imei     = c2.a_imei AND c1.b_phone != c2.a_phone)
        OR  (c1.b_imei     = c2.b_imei AND c1.b_phone != c2.b_phone)
        )
ORDER BY c1.a_imei, c1.call_start_time, c1.call_end_time, c1.b_imei
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top