在John Ruddell的帮助下,我有以下MySQL查询工作得很好。 输出如下:
+-------+--------+-------+-------+-------+-------+-------+------+
| Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | etc..
+-------+--------+-------+-------+-------+-------+-------+------+
| 13050 | 5200 | 48450 | 34041 | 38000 | 0 | 0 | 0 |
+-------+--------+-------+-------+-------+-------+-------+------+
SELECT
t1.Average, t2.weekly_average,
IF(t1.Jan + t2.Jan_webl IS NULL,0,t1.Jan + t2.Jan_webl) AS Jan_Total,
IF(t1.Feb + t2.Feb_webl IS NULL,0,t1.Feb + t2.Feb_webl) AS Feb_Total,
IF(t1.Mar + t2.Mar_webl IS NULL,0,t1.Mar + t2.Mar_webl) AS Mar_Total,
IF(t1.Apr + t2.Apr_webl IS NULL,0,t1.Apr + t2.Apr_webl) AS Apr_Total,
IF(t1.May + t2.May_webl IS NULL,0,t1.May + t2.May_webl) AS May_Total,
IF(t1.Jun + t2.Jun_webl IS NULL,0,t1.Jun + t2.Jun_webl) AS Jun_Total,
IF(t1.Jul + t2.Jul_webl IS NULL,0,t1.Jul + t2.Jul_webl) AS Jul_Total,
IF(t1.Aug + t2.Aug_webl IS NULL,0,t1.Aug + t2.Aug_webl) AS Aug_Total,
IF(t1.Sep + t2.Sep_webl IS NULL,0,t1.Sep + t2.Sep_webl) AS Sep_Total,
IF(t1.Oct + t2.Oct_webl IS NULL,0,t1.Oct + t2.Oct_webl) AS Oct_Total,
IF(t1.Nov + t2.Nov_webl IS NULL,0,t1.Nov + t2.Nov_webl) AS Nov_Total,
IF(t1.Dec + t2.Dec_webl IS NULL,0,t1.Dec + t2.Dec_webl) AS Dec_Total,
t1.Total, t2.total_webl
FROM
( SELECT
AVG(febl_remaining) as 'Average',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%01') THEN febl_remaining END) 'Jan',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%02') THEN febl_remaining END) 'Feb',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%03') THEN febl_remaining END) 'Mar',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%04') THEN febl_remaining END) 'Apr',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%05') THEN febl_remaining END) 'May',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%06') THEN febl_remaining END) 'Jun',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%07') THEN febl_remaining END) 'Jul',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%08') THEN febl_remaining END) 'Aug',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%09') THEN febl_remaining END) 'Sep',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%10') THEN febl_remaining END) 'Oct',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%11') THEN febl_remaining END) 'Nov',
MAX(CASE febl_month WHEN DATE_FORMAT(NOW(), '%Y-%12') THEN febl_remaining END) 'Dec',
SUM(febl_remaining) as 'Total'
FROM
( SELECT
1 as id,
DATE_FORMAT(tblcom.omActCompDate, '%Y-%m') AS febl_Month,
IF
( SUM(DISTINCT weEstBuildLgth) IS NULL,
SUM(DISTINCT tblcom.omEstBuildLgth),
SUM(DISTINCT tblcom.omEstBuildLgth) - SUM(DISTINCT weEstBuildLgth)
) AS febl_remaining
FROM tblweeklyebl
RIGHT OUTER JOIN tblcom ON tblweeklyebl.comID = tblcom.omID
WHERE tblcom.omstatusID Like 'Closed'
GROUP BY DATE_FORMAT(tblcom.omActCompDate, '%Y-%m')
) t
GROUP BY t.id
) t1
JOIN
( SELECT
SUM(DISTINCT tblcom.omEstBuildLgth - tblweeklyebl.weEstBuildLgth) /
COUNT(DISTINCT
IF(tblcom.omEstBuildLgth - tblweeklyebl.weEstBuildLgth = 0, NULL, MONTH(tblweeklyebl.weDate))
) AS weekly_Average,
SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%01') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Jan_webl',
SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%02') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Feb_webl',
SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%03') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Mar_webl',
SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%04') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Apr_webl',
SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%05') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'May_webl',
SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%06') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Jun_webl',
SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%07') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Jul_webl',
SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%08') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Aug_webl',
SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%09') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Sep_webl',
SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%10') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Oct_webl',
SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%11') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Nov_webl',
SUM(DISTINCT CASE WHEN DATE_FORMAT(tblweeklyebl.weDate, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%12') THEN (tblweeklyebl.weEstBuildLgth) ELSE 0 END) AS 'Dec_webl',
SUM(DISTINCT tblweeklyebl.weEstBuildLgth) AS Total_webl
FROM tblweeklyebl
RIGHT OUTER JOIN tblcom ON tblweeklyebl.comID = tblcom.omID
) t2

需要以这种方式分组:
Jan 13050
Feb 5200
Mar 48450
Apr 34041
May 38000
Jun 0
Jul 0
Aug 0
Sep 0
Oct 0
Nov 0
Dec 0