我有两张桌子:
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
包含所有日期,所以内部联接应该在这里工作吗?)
要关闭,我已经尝试在下面运行以下查询,它只是搅拌一两分钟后没有结果。关于如何使这个工作的任何想法?预先感谢您的帮助! : - )
(运行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
答案 0 :(得分:0)
在这种情况下,你绝对不应该加入约会。
例如,您可能拥有以下数据:
deskcases ringcentral
--------- -----------
date | whatever_d date | whatever_r
---------------------- ----------------------
today x yesterday u
yesterday y
yesterday z
现在,当你加入他们时,你会得到这个:
joinedTable
-----------
date | whatever_d | whatever_r
-------------------------------------
today x
yesterday y u
yesterday z u
哎呀,你从铃声表中得到了2次,但你只想要1.
那么,你如何得到你的数据?写下2个语句或将它们与UNION
组合:
SELECT
DATE(`Resolved At`) AS Date1,
"Deskcases" AS fromTable,
COUNT(*) AS TotalNumber
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` > CURDATE()- INTERVAL 8 DAY
GROUP BY 1, 2
UNION ALL
SELECT
`Date` AS Date1,
"ringcentral" AS fromTable,
COUNT(*) AS TotalNumber
FROM
ringcentral
WHERE
`ringcentral`.`Type` = 'Voice'
AND `Action Result` = 'Accepted'
AND ringcentral.`Date` > CURDATE()- INTERVAL 8 DAY
GROUP BY 1, 2
另请注意,我已将NOW()
更改为CURDATE()
。在deskcases表中,您的日期列似乎是一个时间戳。当您从'2013-03-20 10:06:00'减去8天后,您将获得'2013-03-12 10:06:00',因此您将丢失'2013-03-12之间的所有行00:00:00'和'2013-03-12 10:05:59'。我还使用了DATE()
函数而不是DATE_FORMAT()
。 DATE_FORMAT()
在许多方面都是非常好用且有用的功能,但在这种情况下,使用DATE()
时必须输入较少的字符:)
<强>更新强>
SELECT
Date1,
SUM(CASE WHEN fromTable = 'Deskcases' THEN TotalNumber ELSE 0 END) AS TotalDeskcases,
SUM(CASE WHEN fromTable = 'ringcentral' THEN TotalNumber ELSE 0 END) AS TotalRingcentral
FROM (
SELECT
DATE(`Resolved At`) AS Date1,
'Deskcases' AS fromTable,
COUNT(*) AS TotalNumber
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` > CURDATE()- INTERVAL 8 DAY
GROUP BY 1, 2
UNION ALL
SELECT
`Date` AS Date1,
'ringcentral' AS fromTable,
COUNT(*) AS TotalNumber
FROM
ringcentral
WHERE
`ringcentral`.`Type` = 'Voice'
AND `Action Result` = 'Accepted'
AND ringcentral.`Date` > CURDATE()- INTERVAL 8 DAY
GROUP BY 1, 2
) sq
GROUP BY Date1