嗨,我有一张如下所示的表格:
Table Name: Emails
ID |CreatedDate |finalStatus
115173922 |2013-04-09 12:33:23.234 |DELIVERED
115123432 |2013-04-09 08:21:23.234 |FAILED
115143212 |2013-04-09 12:24:23.234 |DELIVERED
115173922 |2013-04-09 05:05:23.234 |DELIVERED
111233922 |2013-04-10 12:44:23.234 |PENDING
115123912 |2013-04-10 12:05:23.234 |DELIVERED
115173922 |2013-04-11 22:09:23.234 |DELIVERED
111233922 |2013-04-11 13:05:23.234 |PENDING
115123912 |2013-04-11 05:23:23.234 |DELIVERED
我需要做的是获得该月每天交付、失败和待处理的 finalStatus 的总量。我试图修改人们在以前的答案中给出的 MySQL 代码,例如:SQL query for Calculating Total No. of Orders per Day?但一直无法让它工作。
这是我到目前为止的代码:
SELECT DISTINCT (CAST(CreatedDate as DATE)) as Date,
(SELECT COUNT(finalStatus)
FROM [Emails]
WHERE finalStatus = 'DELIVERED') AS Delivered,
(SELECT COUNT(finalStatus)
FROM [Emails]
WHERE finalStatus = 'FAILED') AS Failed,
(SELECT COUNT(finalStatus)
FROM [Emails]
WHERE finalStatus = 'PENDING') AS Pending
FROM [Emails]
GROUP BY (CAST(CreatedDate as DATE))
如果有人可以帮助我,那就太棒了。我已经坚持了几个小时了,可能很快就会发疯......