t-sql 查找客户服务中的重复呼叫者

t-sql to find repeated callers in customer care

我有这样的情况,我需要找出当月的 2 天内用相同号码呼叫客户服务的号码。例如:

Number      dateTime
0987654321  2015-06-16 16:16:13.877
0987654321  2015-06-15 12:16:13.877
0789386834  2015-06-01 16:16:13.877
0789386834  2015-06-16 16:16:13.877
0987654321  2015-06-01 12:16:13.877
0123456789  2015-06-01 12:16:13.877
0123456789  2015-06-06 12:16:13.877
0123456789  2015-06-16 12:16:13.877

在此,我需要捕获他在 15 日和 16 日(相差不到 2 天)打来的电话号码 0987654321。休息,0123456789 和 0789386834 将不会包含在结果中,因为他们没有在 2 天内拨打电话。我正在尝试使用 CTE,但它说内存异常。

PS: 超过200万条记录

我得到了这个查询的预期结果:

-- dummy table   
DECLARE @tab TABLE
    (
        number VARCHAR(100),
        timeofcall DATETIME
    )

--insert sample values
    INSERT INTO @tab 
    VALUES
    ('0987654321','2015-06-16 16:16:13.877'),
    ('0987654321','2015-06-15 12:16:13.877'),
    ('0789386834','2015-06-01 16:16:13.877'),
    ('0789386834','2015-06-16 16:16:13.877'),
    ('0987654321','2015-06-01 12:16:13.877'),
    ('0123456789','2015-06-01 12:16:13.877'),
    ('0123456789','2015-06-06 12:16:13.877'),
    ('0123456789','2015-06-16 12:16:13.877')

--get records who are present in the table within 2 days
    SELECT  *
    FROM    @tab t
    WHERE   EXISTS(SELECT   TOP 1 1 
                    FROM    @tab t2 
                    WHERE   t2.number = t.number 
                            AND t2.timeofcall <> t.timeofcall
                            AND ABS(DATEDIFF(DAY,t2.timeofcall,t.timeofcall)) <= 2)