0

我正在尝试将客户的呼叫标记为重复,如果他们在接下来的 5 天内拨打了之前的电话。如果他们在前 5 天内接到了多个电话,我希望它显示为计数。我可以为静态日期范围执行此操作,但不确定如何为相对日期范围执行此操作。

我有客户 ID 和通话日期,但希望添加重复计数列。

客户 ID、通话日期、重复

453, 01-11-14, 0

453, 13-11-14, 0

351, 01-DEC-14, 0

453, 15-11-14, 1

441, 02-DEC-14, 0

102, 03-DEC-14, 0

453, 03-DEC-14, 0

441, 05-DEC-14, 1

453, 05-DEC-14, 1

453, 06-DEC-14, 2

我在这里看到了基本相同问题的答案,但是它使用了我理解的交叉应用功能,这在 Oracle 中是不可用的; SQL 通过滑动窗口记录出现次数

4

2 回答 2

1

您可以使用相关子查询来执行此操作:

select CustomerId, CallDate, 
       (select count(*)
        from table t2
        where t2.CusomerId = t.CustomerId and
              t2.CallDate >= t.CallDate - 5 and t2.CallDate < t.CallDate
       ) as Repeat
from table t;
于 2014-12-11T12:32:41.603 回答
0
/*
WITH tab AS (
SELECT 453 CustomerID, to_date('01-11-14', 'dd-mm-yy') CallDate FROM dual
UNION ALL SELECT 453, to_date('13-11-14', 'dd-mm-yy') FROM dual
union all select 351, to_date('01-12-14', 'dd-mm-yy') FROM dual
UNION ALL SELECT 453, to_date('15-11-14', 'dd-mm-yy') FROM dual
union all select 441, to_date('02-12-14', 'dd-mm-yy') FROM dual
UNION ALL SELECT 102, to_date('03-12-14', 'dd-mm-yy') FROM dual
union all select 453, to_date('03-12-14', 'dd-mm-yy') FROM dual
UNION ALL SELECT 441, to_date('05-12-14', 'dd-mm-yy') FROM dual
union all select 453, to_date('05-12-14', 'dd-mm-yy') FROM dual
UNION ALL SELECT 453, to_date('06-12-14', 'dd-mm-yy') FROM dual
)
*/
SELECT CustomerID, CallDate, 
       count(CallDate) OVER(PARTITION BY CustomerID ORDER BY CallDate RANGE BETWEEN 5 PRECEDING AND CURRENT ROW) - 1 repeat_count
FROM tab order by 2;
于 2014-12-11T12:26:21.463 回答