StackOverflow上的好人,
请非常友好地提供一些帮助...
所以我们这里有一个类似的表...包含客户给某个联络中心(HelpDesk或其他)的电话。
-----------------------------------------------------------------------
| DateD | DateM | Date_Time |EMPL_ID| PHONE_NO |FIRST_REP |
|----------------------------------------------------------------------
|2016-12-12| 2016-12-01| 2016-12-12 15:55| 16652 | 123456789| First |
|2016-12-22| 2016-12-01| 2016-12-22 10:42| 18178 | 123456789| First |
|2016-12-22| 2016-12-01| 2016-12-22 10:54|112981 | 123456789| Repeat |
|2016-12-22| 2016-12-01| 2016-12-22 10:57| 18179 | 123456789| Repeat |
|2016-12-23| 2016-12-01| 2016-12-23 12:27| 16653 | 123456789| Repeat |
|2017-01-05| 2017-01-01| 2017-01-05 15:20| 17896 | 123456789| First |
|2017-01-11| 2017-01-01| 2017-01-11 15:48| 17909 | 123456789| Repeat |
|2017-01-18| 2017-01-01| 2017-01-18 10:07| 18175 | 123456789| Repeat |
|2016-12-03| 2016-12-01| 2016-12-03 20:32| 17745 | 111222333| First |
|2016-12-21| 2016-12-01| 2016-12-21 18:47| 10982 | 111222333| First |
|2016-12-22| 2016-12-01| 2016-12-22 15:53| 17820 | 111222333| Repeat |
|2016-12-28| 2016-12-01| 2016-12-28 13:07| 15976 | 111222333| Repeat |
|2016-12-29| 2016-12-01| 2016-12-29 21:35| 17896 | 111222333| Repeat |
|2016-12-29| 2016-12-01| 2016-12-29 21:46| 15498 | 111222333| Repeat |
|2017-01-02| 2017-01-01| 2017-01-02 16:24| 13117 | 111222333| Repeat |
-----------------------------------------------------------------------
我想做的是弄清楚,重复了多少次来电,这意味着客户再次打来电话。
现在棘手的部分是重复呼叫被定义为源自“第一次呼叫”的呼叫,并且在第一次呼叫之后的每次交互的7天时间内连续重复,例如:
----------------------------------------------------------------------------
| DateD | DateM | Date_Time |EMPL_ID| PHONE_NO |FIRST_REP |
|---------------------------------------------------------------------------
|2016-12-01 | 2016-12-12 | 2016-12-12 15:55 | 16652 | 123456789 | First |
|2016-12-01 | 2016-12-22 | 2016-12-22 10:42 | 18178 | 123456789 | First |
|2016-12-01 | 2016-12-22 | 2016-12-22 10:54 | 112981| 123456789 | Repeat |
|2016-12-01 | 2016-12-22 | 2016-12-22 10:57 | 18179 | 123456789 | Repeat |
|2016-12-01 | 2016-12-23 | 2016-12-23 12:27 | 16653 | 123456789 | Repeat |
|2017-01-01 | 2017-01-05 | 2017-01-05 15:20 | 17896 | 123456789 | First |
|2017-01-01 | 2017-01-11 | 2017-01-11 15:48 | 17909 | 123456789 | Repeat |
|2017-01-01 | 2017-01-18 | 2017-01-18 10:07 | 18175 | 123456789 | Repeat |
----------------------------------------------------------------------------
我们有:
第一行是没有重复呼叫的第一次呼叫,
2sd行是第一次呼叫,有3次重复呼叫,因为每次互动都是在从第一次呼叫开始的每个前一天7天的时间跨度内
第3行是第一次通话,有2次重复通话,如上所述。
现在我们要说的是ID为16652的员工(第1行)产生了0次重复呼叫,但另一方面生成了ID 18178的员工3次重复呼叫。
最后,有一些方法可以创建像这样的输出:
| DateM | DateD | Date_Time |EMP_ID | PHONE_NO |FIRST_REP | DateM_REP | DateD_REP | Date_Time_REP | EMP_ID_REP | PHONE_NO_REP | FIRST_REP_REP
|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|2016-12-01 | 2016-12-12 | 2016-12-12 15:55 | 16652 | 123456789 | First | null | null | null | null | null | null
|2016-12-01 | 2016-12-22 | 2016-12-22 10:42 | 18178 | 123456789 | First | 2016-12-01 | 2016-12-22 | 2016-12-22 10:54 | 112981 | 123456789 | Repeat
|2016-12-01 | 2016-12-22 | 2016-12-22 10:42 | 18178 | 123456789 | First | 2016-12-01 | 2016-12-22 | 2016-12-22 10:57 | 18179 | 123456789 | Repeat
|2016-12-01 | 2016-12-22 | 2016-12-22 10:42 | 18178 | 123456789 | First | 2016-12-01 | 2016-12-23 | 2016-12-23 12:27 | 16653 | 123456789 | Repeat
|2017-01-01 | 2017-01-05 | 2017-01-05 15:20 | 17896 | 123456789 | First | 2017-01-01 | 2017-01-11 | 2017-01-11 15:48 | 17909 | 123456789 | Repeat
|2017-01-01 | 2017-01-05 | 2017-01-05 15:20 | 17896 | 123456789 | First | 2017-01-01 | 2017-01-18 | 2017-01-18 10:07 | 18175 | 123456789 | Repeat
请帮忙,我不擅长写CTE,因为我认为这是一个有可能通过CTE解决的问题。
很多人不屑一顾 卢基的编辑:
CREATE TABLE t_calls
(
[DateM] date,
[DateD] date,
[Date_Time] datetime2(7),
[EMPL_ID] int,
[INTERACTION_ID] numeric(25,0),
[PHONE_NO] numeric(9,0),
[FIRST_REP] varchar(10)
)
Insert Into t_calls
([DateM],[DateD],[Date_Time],[EMPL_ID],[INTERACTION_ID],[PHONE_NO],[FIRST_REP])
Values
('2016-12-01 00:00:00','2016-12-12 00:00:00','2016-12-12 15:55:36',16652,340680165,123456789,'First')
,('2016-12-01 00:00:00','2016-12-22 00:00:00','2016-12-22 10:42:45',18178,343736497,123456789,'First')
,('2016-12-01 00:00:00','2016-12-22 00:00:00','2016-12-22 10:54:46',112981,343750151,123456789,'Repeat')
,('2016-12-01 00:00:00','2016-12-22 00:00:00','2016-12-22 10:57:29',18179,343750151,123456789,'Repeat')
,('2016-12-01 00:00:00','2016-12-23 00:00:00','2016-12-23 12:27:56',16653,344071359,123456789,'Repeat')
,('2017-01-01 00:00:00','2017-01-05 00:00:00','2017-01-05 15:20:47',17896,347063121,123456789,'First')
,('2017-01-01 00:00:00','2017-01-11 00:00:00','2017-01-11 15:48:20',17909,348429965,123456789,'Repeat')
,('2017-01-01 00:00:00','2017-01-18 00:00:00','2017-01-18 10:07:45',18175,350243945,123456789,'Repeat')
,('2016-12-01 00:00:00','2016-12-03 00:00:00','2016-12-03 20:32:37',17745,338392721,111222333,'First')
,('2016-12-01 00:00:00','2016-12-21 00:00:00','2016-12-21 18:47:12',10982,343633967,111222333,'First')
,('2016-12-01 00:00:00','2016-12-22 00:00:00','2016-12-22 15:53:59',17820,343885389,111222333,'Repeat')
,('2016-12-01 00:00:00','2016-12-28 00:00:00','2016-12-28 13:07:19',15976,344944219,111222333,'Repeat')
,('2016-12-01 00:00:00','2016-12-29 00:00:00','2016-12-29 21:35:44',17896,345396945,111222333,'Repeat')
,('2016-12-01 00:00:00','2016-12-29 00:00:00','2016-12-29 21:46:43',15498,345398005,111222333,'Repeat')
,('2017-01-01 00:00:00','2017-01-02 00:00:00','2017-01-02 16:24:12',13117,346045147,111222333,'Repeat')
答案 0 :(得分:0)
如果我理解您的问题,您希望每次通话都知道什么,请在7天内重复多少次通话。
SELECT
a.date_time
,a.emp_id
,a.phone_no
,count(b.phone_no) as repeat_calls --count any non-null field
,min(b.date_time) as first_repeat_call_at
FROM t_calls a
LEFT JOIN t_calls b
ON a.phone_no = b.phone_no --same phone
AND datediff(d, a.date_time, b.date_time) between 0 AND 6 --a repeat comes in today + 6 days
AND a.date_time < b.date_time --prevents self join
GROUP BY
a.date_time
,a.emp_id
,a.phone_no
对于任何0次重复的调用,没有什么可以加入,因此无需计算,因此repeat_calls
= 0且first_repeat_call_at
为NULL。