如何编写SQL查询记录呼叫中心操作?

时间:2018-10-30 18:58:29

标签: sql sql-server

我有两个表:

表1 历史

--------------------------------------------------------------------
Callplacetime            | Finish Code         | Address   | Identidy                                
2018-09-26 16:29:08:000  | No Answer           | ABC       | 10                                  
2018-10-15 12:37:08:000  | Voicemail           | ABC       | 10                                 
2018-10-16 18:40:09:000  | No Answer           | ABC       | 10                                
2018-10-16 21:29:08:000  | No Answer           | ABC       | 10                                  
2018-10-19 04:57:10:000  | Appointment Booked  | ABC       | 10                                
2018-09-26 16:29:57:000  | No Answer           | XYZ       | 20                                 
2018-10-15 16:29:08:000  | Voicemail           | XYZ       | 20                                
2018-10-15 18:29:08:000  | Appointment Booked  | XYZ       | 20                                
2018-10-25 16:29:08:000  | Appointment Booked  | MNO       | 30                                 

表2 列表

---------------------------------------------
Address  |   Identity  
ABC      |   10
XYZ      |   20
MNO      |   30

我希望数据显示为

Callplacetime            | Finish Code         | Address   | Attempts by Agents
2018-09-26 16:29:08:000  | No Answer           | ABC       | Attemp1 
2018-10-15 12:37:08:000  | Voicemail           | ABC       | Attemp2
2018-10-16 18:40:09:000  | No Answer           | ABC       | Attemp3
2018-10-16 21:29:08:000  | No Answer           | ABC       | Attemp4  
2018-10-19 04:57:10:000  | Appointment Booked  | ABC       | Attemp5
2018-09-26 16:29:57:000  | No Answer           | XYZ       | Attemp1 
2018-10-15 16:29:08:000  | Voicemail           | XYZ       | Attemp2
2018-10-15 18:29:08:000  | Appointment Booked  | XYZ       | Attemp3
2018-10-25 16:29:08:000  | Appointment Booked  | MNO       | Attemp1 

因此,基本上,座席会调用特定的地址(客户)。因此,如果他仅呼叫该地址一次,则“代理尝试次数”列如果第二次呼叫相同地址,则说“尝试1”,然后尝试“尝试2”,依此类推。座席最多可以尝试5次(五次)。当然,最近的“呼叫时间”将是座席的最新尝试。

如何根据上述情况编写查询,以按“代理商”列添加尝试?

2 个答案:

答案 0 :(得分:0)

您可以使用窗口函数和字符串连接。

select *, 
'Attempt' + cast(row_number() over (partition by Address order by Callplacetime) as varchar)
from [History]

请注意,我不确定List表的用途。我希望您需要加入它,以基于Address表中的Identity来获取History……但是您将其包括在History表中,因此我不确定这里的ERD是什么样的。

答案 1 :(得分:-1)

你可以喜欢

CREATE TABLE T
    ([Callplacetime] datetime, [Finish Code] varchar(18), [Address] varchar(3), [Identidy] int)
;

INSERT INTO T
    ([Callplacetime], [Finish Code], [Address], [Identidy])
VALUES
    ('2018-09-26 16:29:08', 'No Answer', 'ABC', 10),
    ('2018-10-15 12:37:08', 'Voicemail', 'ABC', 10),
    ('2018-10-16 18:40:09', 'No Answer', 'ABC', 10),
    ('2018-10-16 21:29:08', 'No Answer', 'ABC', 10),
    ('2018-10-19 04:57:10', 'Appointment Booked', 'ABC', 10),
    ('2018-09-26 16:29:57', 'No Answer', 'XYZ', 20),
    ('2018-10-15 16:29:08', 'Voicemail', 'XYZ', 20),
    ('2018-10-15 18:29:08', 'Appointment Booked', 'XYZ', 20),
    ('2018-10-25 16:29:08', 'Appointment Booked', 'MNO', 30);

SELECT Callplacetime,
       [Finish Code],
       Address,
       'Attempt' + 
       CAST(ROW_NUMBER() OVER (PARTITION BY Identidy ORDER BY Callplacetime) AS VARCHAR(10)) [Attempts by Agents]
FROM T;

返回:

+---------------------+--------------------+---------+--------------------+
|    Callplacetime    |    Finish Code     | Address | Attempts by Agents |
+---------------------+--------------------+---------+--------------------+
| 26/09/2018 16:29:08 | No Answer          | ABC     | Attempt1           |
| 15/10/2018 12:37:08 | Voicemail          | ABC     | Attempt2           |
| 16/10/2018 18:40:09 | No Answer          | ABC     | Attempt3           |
| 16/10/2018 21:29:08 | No Answer          | ABC     | Attempt4           |
| 19/10/2018 04:57:10 | Appointment Booked | ABC     | Attempt5           |
| 26/09/2018 16:29:57 | No Answer          | XYZ     | Attempt1           |
| 15/10/2018 16:29:08 | Voicemail          | XYZ     | Attempt2           |
| 15/10/2018 18:29:08 | Appointment Booked | XYZ     | Attempt3           |
| 25/10/2018 16:29:08 | Appointment Booked | MNO     | Attempt1           |
+---------------------+--------------------+---------+--------------------+

由于您的第一张桌子已经有Address列,所以我看不到第二张桌子的目的是什么?