Actually we can avoid IF condition in the query.
SELECT
dtax, dtotal, itotal
FROM
(SELECT tax dtax, SUM(mrp * qty) AS dTotal
FROM debits d
INNER JOIN debits_ps db_ps ON db_ps.db_fkid = d.db_id
INNER JOIN transfers ts ON ts.ps_fkid = db_ps.ps_fkid
GROUP BY tax) a
LEFT JOIN
(SELECT tax itax, SUM(mrp * qty) AS iTotal
FROM invoices i
INNER JOIN invoice_ps inv_ps ON inv_ps.inv_fkid = i.inv_id
INNER JOIN transfers ts ON ts.ps_fkid = inv_ps.ps_fkid
GROUP BY tax) b
ON dtax = itax
UNION
SELECT
itax, dtotal, itotal
FROM
(SELECT tax dtax, SUM(mrp * qty) AS dTotal
FROM debits d
INNER JOIN debits_ps db_ps ON db_ps.db_fkid = d.db_id
INNER JOIN transfers ts ON ts.ps_fkid = db_ps.ps_fkid
GROUP BY tax) a
RIGHT JOIN
(SELECT tax itax, SUM(mrp * qty) AS iTotal
FROM invoices i
INNER JOIN invoice_ps inv_ps ON inv_ps.inv_fkid = i.inv_id
INNER JOIN transfers ts ON ts.ps_fkid = inv_ps.ps_fkid
GROUP BY tax) b
ON dtax = itax
Jane, we need to play around with left / right joins and the two main queries ( Invoices and Debits ).
Will take the below data for example.
Debits - Output of Debits query
+------+--------+
| dtax | dTotal |
+------+--------+
| 0% | 18 |
| 5% | 120 |
+------+--------+
Invoices - Output of Invoices query
+------+--------+
| itax | iTotal |
+------+--------+
| 0% | 150 |
| 15% | 80 |
+------+--------+
Debits Left join with Invoices gives you all the tax wise debits and their matching invoices.
+------+--------+--------+
| dtax | dtotal | itotal |
+------+--------+--------+
| 0% | 18 | 150 |
| 5% | 120 | NULL |
+------+--------+--------+
Debits Right join with Invoices will give you all the tax wise invoices and their matching debits.
+------+--------+--------+
| itax | dtotal | itotal |
+------+--------+--------+
| 0% | 18 | 150 |
| 15% | NULL | 80 |
+------+--------+--------+
UNION is used to combine these two results into unique records. i.e duplicate records are removed.
+------+--------+--------+
| dtax | dtotal | itotal |
+------+--------+--------+
| 0% | 18 | 150 |
| 5% | 120 | NULL |
| 15% | NULL | 80 |
+------+--------+--------+
NOTE: If you use UNION ALL, the duplicate records will not be removed. Just try with UNION ALL. You can find the difference.
Query to find tax wise invoice-debit :
select dtax, if(itotal is null, 0, itotal)-if(dtotal is null, 0, dtotal) as `invoice-debit` from (SELECT dtax, dtotal, itotal FROM (SELECT tax dtax, SUM(mrp * qty) AS dTotal FROM debits d INNER JOIN debits_ps db_ps ON db_ps.db_fkid = d.db_id INNER JOIN transfers ts ON ts.ps_fkid = db_ps.ps_fkid GROUP BY tax) a LEFT JOIN (SELECT tax itax, SUM(mrp * qty) AS iTotal FROM invoices i INNER JOIN invoice_ps inv_ps ON inv_ps.inv_fkid = i.inv_id INNER JOIN transfers ts ON ts.ps_fkid = inv_ps.ps_fkid GROUP BY tax) b ON dtax = itax UNION SELECT itax, dtotal, itotal FROM (SELECT tax dtax, SUM(mrp * qty) AS dTotal FROM debits d INNER JOIN debits_ps db_ps ON db_ps.db_fkid = d.db_id INNER JOIN transfers ts ON ts.ps_fkid = db_ps.ps_fkid GROUP BY tax) a RIGHT JOIN (SELECT tax itax, SUM(mrp * qty) AS iTotal FROM invoices i INNER JOIN invoice_ps inv_ps ON inv_ps.inv_fkid = i.inv_id INNER JOIN transfers ts ON ts.ps_fkid = inv_ps.ps_fkid GROUP BY tax) b ON dtax = itax) x;
You need to use UNION to combine the records. Otherwise you will not get both invoice and debits.