我有两张桌子:
deskcases
:| CaseID | Resolved At(Date) | Labels |
ringcentral
:| Date | Type | Action Result |
我的目标是在前 7 天获得以下输出:
| Date | Count of TotalResolvedCases | Count of Total Calls |
我需要在两个单独的表中计算案例和呼叫,然后按日期加入和分组计数。我还在处理两个表中不同的日期格式,因此古怪的 group by 和 join 函数。
我还需要让加入包括前 7 天的所有日期(deskcases
有所有日期,所以我认为 INNER JOIN 应该在这里工作吗?)
要关闭,我尝试在下面运行以下查询,但在一两分钟后它只是搅动而没有结果。关于如何使它工作的任何想法?预先感谢您的帮助!:-)
(运行 MySQL 5.0.96)
SELECT
DATE_FORMAT(
deskcases.`Resolved At`,
'%Y-%m-%d'
)AS Date1,
(
SELECT
COUNT(deskcases.`Case #`)
FROM
deskcases
WHERE
deskcases.Labels NOT LIKE '%SPAM%'
AND deskcases.Labels NOT LIKE '%Online Orders%'
AND deskcases.Labels NOT LIKE '%Internal SPAM%'
AND deskcases.`Resolved At` > NOW()- INTERVAL 8 DAY
)AS TotalResolvedCases,
(
SELECT
COUNT(ringcentral.Counter)
FROM
ringcentral
WHERE
`ringcentral`.`Type` = 'Voice'
AND `Action Result` = 'Accepted'
AND ringcentral.`Date` > NOW()- INTERVAL 8 DAY
)AS TotalCalls
FROM
deskcases
INNER JOIN ringcentral ON DATE_FORMAT(
deskcases.`Resolved At`,
'%Y-%m-%d'
)= ringcentral.Date
GROUP BY
Date1
ORDER BY
Date1 ASC
编辑
我可以稍微编辑一下以给我一些结果,但似乎我正在计算所有案例和使用我的子选择查询的调用,并且总数没有按日期分组。这是代码:
SELECT
DATE_FORMAT(
deskcases.`Resolved At`,
'%Y-%m-%d'
)AS Date1,
(
SELECT
COUNT(deskcases.`Case #`)
FROM
deskcases
WHERE
deskcases.Labels NOT LIKE '%SPAM%'
AND deskcases.Labels NOT LIKE '%Online Orders%'
AND deskcases.`Case Status` LIKE '%Resolved%'
)AS TotalResolvedCases,
(
SELECT
COUNT(ringcentral.Counter)
FROM
ringcentral
WHERE
`ringcentral`.`Type` = 'Voice'
AND `Action Result` = 'Accepted'
)AS TotalCalls
FROM
deskcases
LEFT OUTER JOIN ringcentral ON DATE_FORMAT(
deskcases.`Resolved At`,
'%Y-%m-%d'
)= ringcentral.Date
WHERE deskcases.`Resolved At` > NOW()- INTERVAL 8 DAY
GROUP BY
Date1
ORDER BY
Date1 ASC