累计税收MYSQL总和

时间:2011-07-28 15:33:37

标签: mysql sql

我需要在我的申请中支持累积税。我允许我的物品支持多种税(1或2)。目前,我计算总税额的方式是将每个税收百分比(例如4和8)相加,并收取12%的税。但是,对于累积税,您需要第二个税额按(小计+税1金额)*税2%计算。如何在一个查询中使用以下表结构在mysql中执行此操作。 (我需要计算的是总税额)

mysql> describe phppos_sales_items;
+--------------------+---------------+------+-----+---------+-------+
| Field              | Type          | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+---------+-------+
| sale_id            | int(10)       | NO   | PRI | 0       |       |
| item_id            | int(10)       | NO   | PRI | 0       |       |
| description        | varchar(255)  | YES  |     | NULL    |       |
| serialnumber       | varchar(255)  | YES  |     | NULL    |       |
| line               | int(3)        | NO   | PRI | 0       |       |
| quantity_purchased | double(15,2)  | NO   |     | 0.00    |       |
| item_cost_price    | decimal(15,2) | NO   |     | NULL    |       |
| item_unit_price    | double(15,2)  | NO   |     | NULL    |       |
| discount_percent   | int(11)       | NO   |     | 0       |       |
+--------------------+---------------+------+-----+---------+-------+
9 rows in set (0.00 sec)

mysql> describe phppos_sales_items_taxes;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| sale_id    | int(10)      | NO   | PRI | NULL    |       |
| item_id    | int(10)      | NO   | PRI | NULL    |       |
| line       | int(3)       | NO   | PRI | 0       |       |
| name       | varchar(255) | NO   | PRI | NULL    |       |
| percent    | double(15,2) | NO   | PRI | NULL    |       |
| cumulative | int(1)       | NO   |     | 0       |       |
+------------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

这是我当前的查询,它不会使累积标志生效:

CREATE TEMPORARY TABLE phppos_sales_items_temp (SELECT phppos_sales.deleted as deleted, date(sale_time) as sale_date, phppos_sales_items.sale_id, comment,payment_type, customer_id, employee_id, phppos_items.item_id, NULL as item_kit_id, supplier_id, quantity_purchased, item_cost_price, item_unit_price, category, SUM(percent) as item_tax_percent, discount_percent, (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) as subtotal, phppos_sales_items.line as line, serialnumber, phppos_sales_items.description as description, ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(1+(SUM(percent)/100)),2) as total, ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(percent)/100),2) as tax, (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) - (item_cost_price*quantity_purchased) as profit FROM phppos_sales_items INNER JOIN phppos_sales ON phppos_sales_items.sale_id=phppos_sales.sale_id INNER JOIN phppos_items ON phppos_sales_items.item_id=phppos_items.item_id LEFT OUTER JOIN phppos_suppliers ON phppos_items.supplier_id=phppos_suppliers.person_id LEFT OUTER JOIN phppos_sales_items_taxes ON phppos_sales_items.sale_id=phppos_sales_items_taxes.sale_id and phppos_sales_items.item_id=phppos_sales_items_taxes.item_id and phppos_sales_items.line=phppos_sales_items_taxes.line WHERE date(sale_time) BETWEEN "2011-07-28" and "2011-07-28" GROUP BY sale_id, item_id, line) UNION ALL (SELECT phppos_sales.deleted as deleted, date(sale_time) as sale_date, phppos_sales_item_kits.sale_id, comment,payment_type, customer_id, employee_id, NULL as item_id, phppos_item_kits.item_kit_id, '' as supplier_id, quantity_purchased, item_kit_cost_price, item_kit_unit_price, category, SUM(percent) as item_tax_percent, discount_percent, (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100) as subtotal, phppos_sales_item_kits.line as line, '' as serialnumber, phppos_sales_item_kits.description as description, ROUND((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(1+(SUM(percent)/100)),2) as total, ROUND((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(SUM(percent)/100),2) as tax, (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100) - (item_kit_cost_price*quantity_purchased) as profit FROM phppos_sales_item_kits INNER JOIN phppos_sales ON phppos_sales_item_kits.sale_id=phppos_sales.sale_id INNER JOIN phppos_item_kits ON phppos_sales_item_kits.item_kit_id=phppos_item_kits.item_kit_id LEFT OUTER JOIN phppos_sales_item_kits_taxes ON phppos_sales_item_kits.sale_id=phppos_sales_item_kits_taxes.sale_id and phppos_sales_item_kits.item_kit_id=phppos_sales_item_kits_taxes.item_kit_id and phppos_sales_item_kits.line=phppos_sales_item_kits_taxes.line WHERE date(sale_time) BETWEEN "2011-07-28" and "2011-07-28" GROUP BY sale_id, item_kit_id, line) ORDER BY sale_id, line

输出:

+---------+------------+---------+---------+-----------------------+-------------+-------------+---------+-------------+-------------+--------------------+-----------------+-----------------+-----------+------------------+------------------+-------------+------+--------------+--------------------+---------+--------+------------+
| deleted | sale_date  | sale_id | comment | payment_type          | customer_id | employee_id | item_id | item_kit_id | supplier_id | quantity_purchased | item_cost_price | item_unit_price | category  | item_tax_percent | discount_percent | subtotal    | line | serialnumber | description        | total   | tax    | profit     |
+---------+------------+---------+---------+-----------------------+-------------+-------------+---------+-------------+-------------+--------------------+-----------------+-----------------+-----------+------------------+------------------+-------------+------+--------------+--------------------+---------+--------+------------+
|       0 | 2011-07-28 |       1 |         | Cash: $1,416.00<br /> |        NULL |           1 |       1 |        NULL | NULL        |               1.00 |          800.00 |         1200.00 | Computers |            18.00 |                0 | 1200.000000 |    1 |              | Best Computer ever | 1416.00 | 216.00 | 400.000000 |
|       0 | 2011-07-28 |       2 |         | Cash: $2,832.00<br /> |        NULL |           1 |    NULL |           1 |             |               1.00 |         1600.00 |         2400.00 | Computers |            18.00 |                0 | 2400.000000 |    1 |              |                    | 2832.00 | 432.00 | 800.000000 |
|       0 | 2011-07-28 |       3 |         | Cash: $14.52<br />    |        NULL |           1 |       4 |        NULL | NULL        |               1.00 |           10.00 |           12.00 | Test      |            20.00 |                0 |   12.000000 |    1 |              |                    |   14.40 |   2.40 |   2.000000 |
|       0 | 2011-07-28 |       4 |         | Cash: $2,904.00<br /> |        NULL |           1 |    NULL |           1 |             |               1.00 |         1600.00 |         2400.00 | Computers |            20.00 |                0 | 2400.000000 |    1 |              |                    | 2880.00 | 480.00 | 800.000000 |
+---------+------------+---------+---------+-----------------------+-------------+-------------+---------+-------------+-------------+--------------------+-----------------+-----------------+-----------+------------------+------------------+-------------+------+--------------+--------------------+---------+--------+------------+

1 个答案:

答案 0 :(得分:1)

您可以使用旋转技术在2个不同的列中获取累积和正常百分比

SELECT a.sale_id, a.item_id, a.line, 
IFNULL(MAX(CASE WHEN b.cumulative = 1 THEN b.percent END),0) as cum_pcnt,
IFNULL(MAX(CASE WHEN b.cumulative != 1 THEN b.percent END),0) as norm_pcnt
FROM phppos_sales_items a
INNER JOIN phppos_sales_items_taxes b ON 
(b.sale_id = a.sale_id AND b.item_id = a.item_id AND b.line = a.line)
GROUP BY a.sale_id, a.item_id, a.line

现在你可以使用上面的查询作为派生表并加入它而不是phppos_sales_items_taxes表(以下查询只是为了演示方法,我绝对错过了你如何计算成本和转换百分比到乘数的关键点):

SELECT ..., SUM([your cost calculation]*sales_taxes.norm_pcnt) as tax1,
SUM([your cost calculation]*sales_taxes.norm_pcnt*cum_pcnt) as tax2
FROM  ....
INNER[or LEFT, whatever makes sense] JOIN 
([the query above goes here]) sales_taxes 

另外,我强烈建议重构你的桌面结构 - 这些复合PK在你的情况下没有任何意义;如果您需要将字段组合成唯一,则不必将它们作为主键的一部分,而是使用UNIQUE索引/约束。