i have a specific problem. I want to get data for bills. The data will be used for chart so the sum is needed. Here is the explanation:
Sub1 gets the sum of amount for the bills that were paid this month.
Sub2 gets the sum of amount for the bills that are not paid this month.
Sub3 gets the sum of amount for the bills that are calculated as paid for this month, but they are really not paid. That is in case if other company doesn't pay bill for 5 years or more than this bill is "like" paid (court procedures go after that).
The problem is if I have for example no records for sub1, than sub2 or sub3 can't be joined and I want to show them in any case. In case if sub2 has no records and sub3 has, than sub3 is not shown so that is also the problem. How can I show all the records in one row even if there are NULL values for example for sub1 and NOT NULL for sub 2 and sub3, vice versa. Any help would be appreciated since I am working now on this query solution almost all afternoon. :/
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