Question

Here i have 6 tables below. i would like to get invoicetotal and debittotal group by tax% column

invoices
--------
inv_id      col2    col3 
1           xxx     xxx

invoice_ps
-----------
inv_fkid    ps_fkid
1             2
1             3
1             5

packing_slips
--------
ps_id   col2    col3 
1        xxx     xxx
2        xxx     xxx 
3        xxx     xxx 
4        xxx     xxx 
....

debits
--------
db_id   col2    col3 
1       xxx      xxx


debit_ps
-----------
db_fkid     ps_fkid
1               4
1               7
1               6

transfers
---------
ps_fkid     tax   mrp   qty
2           0%    15    10
3           15%   10    5
4           5%    20    6
5           15%   15    2
7           0%    6     3

I can get the invoice total from this

// Invoices
SELECT tax, SUM(mrp * qty) AS Total 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

and for the debit total

// Debits
SELECT tax, SUM(mrp * qty) AS Total FROM debits d
INNER JOIN debit_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

How can i combine the above 2 queries to get the results taxwise as below

tax      invoicetotal - debittotal
0%               xxxx        
5%               xxxx        
15%              xxxx        

thanks for the help

Was it helpful?

Solution

    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.

OTHER TIPS

Something like this will work

  SELECT tax, 
    SUM(case when tag='inv' then mrp * qty else 0 end) AS InvoiceTotal,
    SUM(case when tag='deb' then mrp * qty else 0 end) AS DebitTotal FROM 
  (
  select 'inv' as tag , col_list from invoices 
  union all
  select 'deb' as tag , col_list from debit
  ) as 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 

Hope the below query gives you the desired output.

SELECT 
  IF(dtax IS NOT NULL, dtax, itax) tax, 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 
  IF(dtax IS NOT NULL, dtax, itax) tax, 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top