MYSQL - 多表连接和SUM

时间:2016-05-06 05:29:37

标签: mysql join sum

我在加入三张桌子并获得其价值总和方面遇到问题我有三种类型的表税种类,采购订单和购买物品

目前我正面临获得税务报告的结果,请在下面查看。

Tax Types
id  name        
1   No Tax      
2   VAT @10%    
3   GST @6%     
4   VAT @20%

Purchase Order
id  total   order_tax_id    order_tax   total_tax   grand_total
1   10.0000     1           0.0000      0.0000      10.0000
2   10.0000     2           1.0000      1.0000      11.0000
3   10.0000     3           0.6000      0.6000      10.6000
4   10.0000     4           2.0000      2.0000      12.0000
5   10.0000     1           0.0000      0.9100      10.0000
6   10.0000     1           0.0000      0.5700      10.0000
7   10.0000     1           0.0000      1.6700      10.0000
9   10.0000     2           1.0000      1.5700      11.0000

Purchase Items
id  purchase_id     tax_rate_id     item_tax    tax         subtotal
1   1               1               0.0000      0.0000      10.0000
2   2               1               0.0000      0.0000      10.0000
3   3               1               0.0000      0.0000      10.0000
4   4               1               0.0000      0.0000      10.0000
5   5               2               0.9100      10.0000%    10.0000
6   6               3               0.5700      6.0000%     10.0000
7   7               4               1.6700      20.0000%    10.0000
9   9               3               0.5700      6.0000%     10.0000

输出应该是这样的

tax_name   sum_of_subtotal   sum_of_item_tax   sum_of_grand_total   sum_of_total_tax
VAT @20%    10.00            1.67    10.00     2.00                 3.67
VAT @10%    10.00            1.67    10.00     2.00                 3.67
No Tax      40.00            0.00    0         0                    0.00
GST @6%     10.00            1.67    10.00     2.00                 3.67

请帮助我

我使用的查询

SELECT
tax_rates.name,                
(SUM(purchase_items.subtotal)) AS item_total_amount,
(SUM(purchase_items.item_tax)) AS item_tax_amount,
(SUM(purchases.grand_total) - SUM(purchases.order_tax)) AS total_amount,
(SUM(purchases.order_tax)) AS tax_amount
FROM tax_rates
LEFT JOIN purchases ON purchases.order_tax_id = tax_rates.id 
LEFT JOIN purchase_items ON purchase_items.tax_rate_id = tax_rates.id 
GROUP BY tax_rates.id
ORDER BY  tax_rates.name desc
LIMIT 0, 10

1 个答案:

答案 0 :(得分:0)

我修正了问题

SELECT
tax_rates.name,
purchase_item.subtotal AS item_total_amount,
purchase_item.item_tax AS item_tax_amount,
(purchases.grand_total - purchases.order_tax) AS total_amount,
purchases.order_tax AS tax_amount
FROM tax_rates
LEFT JOIN (SELECT a.order_tax_id, SUM(a.order_tax) as order_tax, SUM(a.grand_total) as grand_total FROM purchases AS a  GROUP BY a.order_tax_id) AS purchases ON purchases.order_tax_id = tax_rates.id    
LEFT JOIN (SELECT a.tax_rate_id, SUM(a.item_tax) as item_tax, SUM(a.subtotal) as subtotal FROM purchase_items AS a  GROUP BY a.tax_rate_id) AS purchase_item ON purchase_item.tax_rate_id = tax_rates.id
GROUP BY tax_rates.id
ORDER BY  tax_rates.name desc
LIMIT 0, 10