使用MySQL计算3个不同表之间的税额

时间:2018-03-23 14:36:35

标签: mysql

我有以下表格结构,并试图从这些表格中做出报告:

___ BillableDatas

|--------|------------|---------|--------------|------------|
| BIL_Id | BIL_Date   |BIL_Rate | BIL_Quantity | BIL_Status |
|--------|------------|---------|--------------|------------|
|      1 | 2018-03-01 |     105 |            1 |    charged |
|      2 | 2018-03-02 |     105 |            1 |  cancelled |
|      3 | 2018-03-01 |      15 |            2 | notcharged |
|      4 | 2018-03-01 |      21 |            1 | notcharged |
|      5 | 2018-03-02 |      15 |            2 | notcharged |
|      6 | 2018-03-02 |      21 |            1 | notcharged |
|--------|------------|---------|--------------|------------|

___ SalesTaxes

|--------|--------------|------------|
| STX_Id | STX_TaxeName | STX_Amount |
|--------|--------------|------------|
|      8 |        Tax 1 |      5.000 |
|      9 |        Tax 2 |      5.000 |
|     10 |        Tax 3 |     19.975 |
|--------|--------------|------------|

STX_Amount是一个百分比。

___ ApplicableTaxes

|-----------|-----------|
| ATX_BILId | ATX_STXId |
|-----------|-----------|
|         1 |         8 |
|         1 |         9 |
|         1 |        10 |
|         2 |         8 |
|         2 |         9 |
|         2 |        10 |
|         3 |         9 |
|         3 |        10 |
|         4 |         9 |
|         5 |         9 |
|         5 |        10 |
|         6 |         9 |
|-----------|-----------|
  • ATX_BILId是___ BillableDatas的商品ID链接。
  • ATX_STXId是与___ SalesTaxes的税号ID链接。

我需要每天得到项目的总和    - 不含税    - 含税

这样的方法就是这样:

|------------------|---------------|------------|
| BIL_RateNonTaxed | BIL_RateTaxed | BIL_Status |
|------------------|---------------|------------|
|           105.00 |        136.47 |    charged | <- Taxes #8, #9 and #10 applicable
|           102.00 |       118.035 | notcharged | <- Taxes #9 and #10 applicable
|------------------|---------------|------------|

对总数的说明:

  • 105 = 105*1 - (收费项目的总和乘以数量)
  • 102 = (15*2)*2+(21*2) - (不收费项目的总和乘以数量)
  • 136.47 = 105+(105*(5+5+19.975)/100)
  • 119.085 = 102+(((15*2)*2)*(5+19.975)/100+(21*2)*5/100)

我的最后一次尝试就是这个:

SELECT 
BIL_Date, 
(BIL_Rate*BIL_Quantity) AS BIL_RateNonTaxed, 
(((BIL_Rate*BIL_Quantity)*SUM(STX_Amount)/100)+BIL_Rate*BIL_Quantity) AS BIL_RateTaxed, 
BIL_Status 
FROM ___BillableDatas 
LEFT JOIN ___SalesTaxes 
ON FIND_IN_SET(STX_Id, BIL_ApplicableTaxes) > 0
LEFT JOIN ___ApplicableTaxes 
ON ___BillableDatas.BIL_Id = ___ApplicableTaxes.ATX_BILId
WHERE BIL_BookingId=1 
GROUP BY BIL_Id AND BIL_Status
ORDER BY BIL_Date 
ASC

如果需要,请参阅此SQLFiddle以帮助您:
http://sqlfiddle.com/#!9/425854f

感谢。

2 个答案:

答案 0 :(得分:0)

SELECT a.BIL_Date, BIL_RateNonTaxed, BIL_RateNonTaxed+BIL_RateTaxed AS BIL_RateTaxed FROM (
    SELECT BIL_Date,  
    SUM(BIL_Rate*BIL_Quantity) AS BIL_RateNonTaxed
    FROM ___BillableDatas 
    WHERE BIL_Status != 'cancelled'
    GROUP BY BIL_Date
 ) a INNER JOIN (
      SELECT BIL_Date, 
      (((BIL_Rate*BIL_Quantity)*SUM(STX_Amount)/100)) AS BIL_RateTaxed
      FROM ___BillableDatas 
      LEFT JOIN ___ApplicableTaxes 
      ON ___BillableDatas.BIL_Id = ___ApplicableTaxes.ATX_BILId
      LEFT JOIN ___SalesTaxes 
      ON STX_Id = ATX_STXId
      WHERE BIL_Status != 'cancelled'
      GROUP BY BIL_Date
  ) b 
ON a.BIL_Date = b.BIL_Date 
ORDER BY a.BIL_Date;

说明: 您的BIL_RateNonTaxed计算未使用___SalesTaxes表,因此它不能出现在查询中,否则会干扰SUM函数。 然而,您的BIL_RateTaxed确实使用了___SalesTaxes表。在那种情况下,我通过创建2个子查询并加入结果来解决。

我知道有更好的答案,但我不熟悉MySQL语法。

答案 1 :(得分:0)

我不忍心使用你的命名政策,所以我自己做了......

DROP TABLE IF EXISTS bills;

CREATE TABLE bills
(bill_id SERIAL PRIMARY KEY
,bill_date DATE NOT NULL
,bill_rate INT NOT NULL
,bill_quantity INT NOT NULL
,bill_status ENUM('charged','cancelled','notcharged')
);

INSERT INTO bills VALUES
(1,'2018-03-01',105,1,'charged'),
(2,'2018-03-02',105,1,'cancelled'),
(3,'2018-03-01',15,2,'notcharged'),
(4,'2018-03-01',21,1,'notcharged'),
(5,'2018-03-02',15,2,'notcharged'),
(6,'2018-03-02',21,1,'notcharged');

DROP TABLE IF EXISTS sales_taxes;

CREATE TABLE sales_taxes
(sales_tax_id SERIAL PRIMARY KEY
,sales_tax_name VARCHAR(12) NOT NULL
,sales_tax_amount DECIMAL(5,3) NOT NULL
);

INSERT INTO sales_taxes VALUES
( 8,'Tax 1', 5.000),
( 9,'Tax 2', 5.000),
(10,'Tax 3',19.975);

DROP TABLE IF EXISTS applicable_taxes;

CREATE TABLE applicable_taxes
(bill_id INT NOT NULL
,sales_tax_id INT NOT NULL
,PRIMARY KEY(bill_id,sales_tax_id)
);

INSERT INTO applicable_taxes VALUES
(1, 8),
(1, 9),
(1,10),
(2, 8),
(2, 9),
(2,10),
(3, 9),
(3,10),
(4, 9),
(5, 9),
(5,10),
(6, 9);

SELECT bill_status
     , SUM(bill_rate*bill_quantity) nontaxed
     , SUM((bill_rate*bill_quantity)+(bill_rate*bill_quantity*total_sales_tax/100)) taxed 
  FROM 
     ( SELECT b.*
            , SUM(t.sales_tax_amount) total_sales_tax 
         FROM bills b 
         JOIN applicable_taxes bt 
           ON bt.bill_id = b.bill_id 
         JOIN sales_taxes t 
           ON t.sales_tax_id = bt.sales_tax_id 
        GROUP  
           BY bill_id
     ) x
 GROUP 
    BY bill_status;
+-------------+---------+-------------+
| bill_status | untaxed | total       |
+-------------+---------+-------------+
| charged     |     105 | 136.4737500 |
| cancelled   |     105 | 136.4737500 |
| notcharged  |     102 | 119.0850000 |
+-------------+---------+-------------+

我的回答与你的回答略有不同,所以我们其中一个人在某个地方犯了错误。无论哪种方式,这都应该让你非常接近。