我有一个调用详细信息表,需要根据日期分组编译摘要查询。问题是单个呼叫ID可以有多行,如下所示。处置是1 =放弃,2 =处理。 RNA =无人接听。
Call ID | Disposition | Call Start | Queue Name | Agent Name | RNA
010001 2 2014-12-03 13:02 Acme_Brick Joe Schmoe FALSE
010002 1 2014-12-03 13:36 Anvils_R_Us Jane Doe TRUE
010003 2 2014-12-03 14:22 Acme_Brick Dan Post TRUE
010003 2 2014-12-03 14:22 Acme_Brick Joe Schmoe FALSE
SET @reportdate = '2014-12-03';
SELECT `Queue Name`,
SUM(CASE WHEN Disposition = 2 THEN 1 ELSE 0 END) AS handled,
COUNT(DISTINCT `Call ID`) AS calls
FROM call_details
WHERE CAST(`Call Start` AS DATE) = @reportdate
GROUP BY `Queue Name`
ORDER BY `Queue Name` ASC;
所以,SUM(CASE WHEN Disposition = 2 THEN 1 ELSE 0 END) AS handled,
导致了我的问题。下面的第一个表是我得到的。第二个是应该是什么。
Queue Name | Handled | Calls
Acme_Brick 3 2
Anvils_R_Us 0 1
Queue Name | Handled | Calls
Acme_Brick 2 2
Anvils_R_Us 0 1
我知道问题与“处置”字段中的CASE
一致;我只是不确定是否必须创建一个中间摘要表,或者如果有一个复合分组,我可以完成,这将找出多行详细信息调用。事先感谢帮助;)。
答案 0 :(得分:3)
使用条件COUNT(DISTINCT删除重复项:
SELECT `Queue Name`,
COUNT(DISTINCT CASE WHEN Disposition = 2 THEN `Call ID` END) AS handled,
COUNT(DISTINCT `Call ID`) AS calls
FROM call_details
WHERE CAST(`Call Start` AS DATE) = @reportdate
GROUP BY `Queue Name`
ORDER BY `Queue Name` ASC;
答案 1 :(得分:1)
使用子查询去除重复项:
SELECT `Queue Name`, SUM(Disposition = 2) AS handled, COUNT(*) AS calls
FROM (
SELECT `Queue Name`, `Call ID`, MAX(Disposition) AS Disposition
FROM call_details
WHERE CAST(`Call Start` AS Date) = @reportdate
GROUP BY `Queue Name`, `Call ID`) AS subq
GROUP BY `Queue Name`
ORDER BY `Queue Name`
答案 2 :(得分:0)
我想到,如果RNA是真的,那么就不应该计算处置。我将CASE语句更改为
SUM(CASE WHEN Disposition = 2 and RNA = 'FALSE' THEN 1 ELSE 0 END) AS handled,
得到了你想要的result。但这是基于您提供的有限数据集。它可能不成立,但取决于您的业务如何规定如何考虑未应答的呼叫。