0

这里我有下面 6 个表。我想按tax%列获取invoicetotaldebittotal

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        

谢谢您的帮助

4

3 回答 3

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 
于 2012-08-06T13:51:19.803 回答
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
于 2012-08-06T14:45:42.460 回答
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.
于 2012-08-07T16:09:12.280 回答