我有一个具体的问题。我想获取账单数据。数据将用于图表,因此需要总和。这是解释:
Sub1 获取本月支付的账单金额总和。
Sub2 获取本月未支付账单的金额总和。
Sub3 获取计算为本月已支付但实际未支付的账单金额总和。这是如果其他公司 5 年或更长时间不支付账单的情况下,该账单是“类似”支付的(法院程序在那之后进行)。
问题是,例如,如果我没有 sub1 的记录,那么 sub2 或 sub3 就无法加入,我想在任何情况下都显示它们。如果 sub2 没有记录而 sub3 有,则 sub3 不会显示,这也是问题所在。即使 sub1 有 NULL 值,sub 2 和 sub3 有 NOT NULL 值,我如何才能在一行中显示所有记录,反之亦然。任何帮助将不胜感激,因为我现在几乎整个下午都在研究这个查询解决方案。:/
SELECT sub1.y,
sub1.m,
sub1.val1,
sub2.val2,
sub2.y,
sub2.m,
sub3.y,
sub3.m,
sub3.val3
FROM
(SELECT YEAR(curdate()) AS y,
MONTH(curdate()) AS m,
SUM(totwdisc) AS val1
FROM pro_partial_inv
WHERE paidd IS NOT NULL
AND bringbar=0
AND YEAR(curdate())=YEAR(paidd)
AND MONTH(curdate())=MONTH(paidd)
GROUP BY y,
m) sub1
INNER JOIN
(SELECT YEAR(curdate()) AS y,
MONTH(curdate()) AS m,
SUM(netto) AS val2
FROM pro_partial_inv
WHERE paidd IS NULL
AND bringbar=0
AND YEAR(curdate())=YEAR(pdate2)
AND MONTH(curdate())=MONTH(pdate2)
GROUP BY y,
m) sub2
LEFT JOIN
(SELECT YEAR(curdate()) AS y,
MONTH(curdate()) AS m,
SUM(totwdisc) AS val3
FROM pro_partial_inv
WHERE paidd IS NOT NULL
AND bringbar=1
AND YEAR(curdate())=YEAR(pdate2)
AND MONTH(curdate())=MONTH(pdate2)
GROUP BY y,
m) sub3
ON sub1.y=sub2.y
AND sub1.m=sub2.m
AND sub2.y=sub3.y
AND sub2.m=sub3.m