このようなクエリがあります
(SELECT s1.m as m,IFNULL(s2.y,2013) as y,IFNULL(s2.s,0) as planned FROM
(SELECT 1 as m
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
UNION SELECT 10
UNION SELECT 11
UNION SELECT 12) s1
LEFT JOIN
(SELECT YEAR(p.pdate2) as y,MONTH(p.pdate2) as m, SUM(p.totwdisc) as s FROM pro_partial_inv p WHERE YEAR(p.pdate2)=2013 GROUP BY y,m) s2
ON s1.m=s2.m)
UNION
(SELECT s5.m as m,IFNULL(s3.y,2013) as y,IFNULL(s3.s,0) as realv FROM
(SELECT 1 as m
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
UNION SELECT 10
UNION SELECT 11
UNION SELECT 12) s5
LEFT JOIN
(SELECT YEAR(p.paidd) as y,MONTH(p.paidd) as m, SUM(p.totwdisc) as s FROM pro_partial_inv p WHERE p.status=2 AND YEAR(p.paidd)=2013 GROUP BY y,m) s3
ON s5.m=s3.m)
UNION
(SELECT s6.m as m,IFNULL(s4.y,2013) as y,IFNULL(s4.s,0) as proj FROM
(SELECT 1 as m
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
UNION SELECT 10
UNION SELECT 11
UNION SELECT 12) s6
LEFT JOIN
(SELECT YEAR(p.startd) as y,MONTH(p.startd) as m, SUM(p.disc_n) as s FROM pro_project p WHERE YEAR(p.startd)=2013 GROUP BY y,m) s4
ON s6.m=s4.m)
したがって、次のようにする必要があります。
m y planned
1 2013 0.00
2 2013 0.00
3 2013 0.00
4 2013 0.00
5 2013 0.00
6 2013 908.18
7 2013 0.00
8 2013 1136.36
9 2013 13354.54
10 2013 0.00
11 2013 0.00
12 2013 0.00
1 2013 0.00
2 2013 0.00
3 2013 0.00
4 2013 0.00
5 2013 0.00
6 2013 908.18
7 2013 0.00
8 2013 1136.36
9 2013 13354.54
10 2013 0.00
11 2013 0.00
12 2013 0.00
1 2013 0.00
2 2013 0.00
3 2013 0.00
4 2013 0.00
5 2013 0.00
6 2013 908.18
7 2013 0.00
8 2013 1136.36
9 2013 13354.54
10 2013 0.00
11 2013 0.00
12 2013 0.00
非常に似ているが異なる 3 つの組合のようなものです。m は月、y 年、および計算による計画値を表します。私が得る結果はこれです:
m y planned
1 2013 0.00
2 2013 0.00
3 2013 0.00
4 2013 0.00
5 2013 0.00
6 2013 908.18
7 2013 0.00
8 2013 1136.36
9 2013 13354.54
10 2013 0.00
11 2013 0.00
12 2013 0.00
6 2013 0.00
7 2013 809.09
8 2013 2227.27
9 2013 0.00
7 2013 43600.00
8 2013 41330.00
私は何か間違ったことをしていますか?おそらくそうですが、なぜここでユニオンが3回作成されないのかわかりません。クエリを別々に実行すると、良い結果が得られます。