我有一个查询报告Asterisk呼叫使用情况和队列统计信息。该查询当前使用MySQL WHERE子句来连接表并过滤数据。
这是查询目前的样子:
SELECT
c.name as 'Agent',
e.extended_number AS 'Extension',
COUNT(ql.`time`) AS 'Total Inbound Calls',
(SELECT
COUNT(1)
FROM
call_history ch,
ast_queue_mstatus qm
WHERE
ch.start >= (DATE_SUB(DATE(NOW()),
INTERVAL 10050 MINUTE))
AND ch.start <= (DATE(NOW()))
AND ch.calltype = 'out'
AND ch.flow = 'out'
AND ch.extension_number = qm.membername
AND qm.membername = e.extended_number
GROUP BY qm.membername) AS 'Total Outbound Calls',
FORMAT(SUM(CAST(ql.arg2 AS UNSIGNED)) / 60, 2) AS 'Total Inbound Duration',
FORMAT(AVG(CAST(ql.arg2 AS UNSIGNED)) / 60, 2) AS 'Avg Inbound Duration',
FORMAT(AVG(CAST(ql.arg1 AS UNSIGNED)) / 60, 2) AS 'Avg Caller Hold Time'
FROM
ast_queue_log ql,
ast_queue_mstatus qs,
client c,
extension e
WHERE
ql.queuename = '1234'
AND ql.`time` >= (DATE_SUB(DATE(NOW()), INTERVAL 7 DAY))
AND ql.`time` <= (DATE(NOW()))
AND (ql.event = 'COMPLETEAGENT'
OR ql.event = 'COMPLETECALLER'
OR ql.event = 'COMPLETETRANSFER')
AND RIGHT(ql.agent, 8) = qs.membername
AND qs.membername = e.extended_number
AND e.client_id = c.id
GROUP BY ql.agent
ORDER BY c.name;
我遇到的问题是我需要为尚未接收任何入站呼叫的队列成员返回出站呼叫结果。目前,如果ast_queue_log
中没有记录,则WHERE
子句会排除记录,这意味着它们不会被标量子查询接收。
如何更改此查询,以便从call_history
获取ast_queue_mstatus
中有相应记录的所有通话记录?
很抱歉,如果这太复杂或信息不足,我可以尝试简化,如果这没有意义!
答案 0 :(得分:1)
现在是时候抛弃那些旧的逗号分隔的表格列表并使用1992年引入的加入标准(是的)。 : - )
因为那时你可以使用LEFT JOIN
。通过这种连接,关系是可选的。也就是说,返回第一个表中的所有行(如果它们与WHERE子句中的条件匹配),但连接表也不必具有记录。
如果没有,则仍会返回一行,但字段会填充NULL
值。
SELECT
c.name as 'Agent',
e.extended_number AS 'Extension',
COUNT(ql.`time`) AS 'Total Inbound Calls',
(SELECT
COUNT(1)
FROM
call_history ch
INNER JOIN ast_queue_mstatus qm
ON qm.membername = ch.extension_number
WHERE
ch.start >= (DATE_SUB(DATE(NOW()), INTERVAL 10050 MINUTE))
AND ch.start <= (DATE(NOW()))
AND ch.calltype = 'out'
AND ch.flow = 'out'
AND ch.extension_number = e.extended_number
GROUP BY
ch.extension_number) AS 'Total Outbound Calls',
FORMAT(SUM(CAST(ql.arg2 AS UNSIGNED)) / 60, 2) AS 'Total Inbound Duration',
FORMAT(AVG(CAST(ql.arg2 AS UNSIGNED)) / 60, 2) AS 'Avg Inbound Duration',
FORMAT(AVG(CAST(ql.arg1 AS UNSIGNED)) / 60, 2) AS 'Avg Caller Hold Time'
FROM
client c,
INNER JOIN extension e
ON e.client_id = c.id
INNER JOIN ast_queue_mstatus qs
ON qs.membername = e.extended_number
LEFT JOIN ast_queue_log ql
ON ql.queuename = '1234'
AND ql.`time` >= (DATE_SUB(DATE(NOW()), INTERVAL 7 DAY))
AND ql.`time` <= (DATE(NOW()))
AND (
ql.event = 'COMPLETEAGENT'
OR ql.event = 'COMPLETECALLER'
OR ql.event = 'COMPLETETRANSFER')
AND RIGHT(ql.agent, 8) = qs.membername
GROUP BY ql.agent
ORDER BY c.name;
您也可以使用RIGHT JOIN
加入相反的方式(第一个表是可选的)。但总的来说,这很难解释。