【发布时间】:2014-02-16 11:21:28
【问题描述】:
您好,我终于想出了如何在单行中获得结果,但我一直坚持获得正确的结果。
以下是我的查询。我正在尝试获取 COUNT 个报告的故障和一个月内完成的故障。当我将 dateFormat(date_reported 更改为 dateFormat(date_completed 时,数字会发生变化,从报告到完成。
select
count(r.rpt) as Reported,
count(c.cpt) as Completed,
DATE_FORMAT(DATE_REPORTED,'%M %Y') as MONTH
from
(select COUNT(faultid) as cpt
from fault_info
where status = 'Completed'
and date_completed >= LAST_DAY(NOW()) - INTERVAL 6 MONTH + INTERVAL 1 DAY
) c,
(select COUNT(faultid) as rpt
from fault_info
where status != 'Completed'
and date_reported >= LAST_DAY(NOW()) - INTERVAL 6 MONTH + INTERVAL 1 DAY
) r,
fault_info
GROUP BY MONTH
ORDER BY date_reported desc
任何帮助将不胜感激!
这是我所追求的输出。但目前我只是为两列得到相同的数字。如上所述,要更改数字,我会更改 DateFormat(date_reported -> DateFormat(date_completed,但数字仍然保持不变。它们对一个是正确的,但对另一个是不正确的。
选择 DateFormat(date_reported...
已报告,已完成,月
“99”、“99”、“2014 年 1 月”
“72”、“72”、“2013 年 12 月”
“71”、“71”、“2013 年 11 月”
“107”、“107”、“2013 年 10 月”
“114”、“114”、“2013 年 9 月”
“112”、“112”、“2013 年 8 月”
或选择 DateFormat(date_completed...
已报告,已完成,月
“68”、“68”、“2014 年 1 月”
“65”、“65”、“2013 年 12 月”
“76”、“76”、“2013 年 11 月”
“113”、“113”、“2013 年 10 月”
“124”、“124”、“2013 年 9 月”
“140”、“140”、“2013 年 8 月”
这样我可以进行百分比比较,我可以查看我们是否正在关闭更多打开的故障。所以我需要一个月的实际报告与一个月的实际关闭。
【问题讨论】:
-
考虑提供适当的 DDL(和/或 sqlfiddle)以及所需的结果集
标签: mysql join union where-clause