mysql使用单个组按列组合来自不同表的数据

时间:2012-08-06 13:44:31

标签: mysql group-by sum inner-join aliasing

这里我有6张桌子。我想通过 tax%列获得 invoicetotal debittotal

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

我可以从此

获取发票总额
// 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

和借记总额

// 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

如何合并上述2个查询以按照以下方式获得结果

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

感谢您的帮助

3 个答案:

答案 0 :(得分:0)

这样的东西会起作用

  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 

答案 1 :(得分:0)

希望以下查询为您提供所需的输出。

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

答案 2 :(得分:0)

    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.