You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求编写MySQL查询:计算有效收费的税费总额并生成指定结果

MySQL Query for Filtered Charge Report with Tax Calculation

Problem Statement

We need to generate a report that includes Selling Date, Charge Type, Amount, and Tax columns, following these strict filtering rules:

  • Exclude any charge records marked as deleted (is_deleted = 1)
  • Exclude records tied to charge types that are marked as deleted
  • Ignore tax types marked as deleted when calculating total tax, but still keep displaying the corresponding charge type even if all its taxes are deleted

Table Structures

CREATE TABLE `charge` ( 
  `charge_id` int(11) NOT NULL AUTO_INCREMENT, 
  `selling_date` date DEFAULT NULL, 
  `amount` decimal(10,2) DEFAULT NULL, 
  `is_deleted` tinyint(1) NOT NULL DEFAULT '0', 
  `charge_type_id` int(11) DEFAULT NULL, 
  PRIMARY KEY (`charge_id`) 
);

CREATE TABLE `charge_type` ( 
  `charge_type_id` int(11) NOT NULL AUTO_INCREMENT, 
  `charge_type` varchar(20) NOT NULL, 
  `is_deleted` tinyint(1) DEFAULT '0', 
  PRIMARY KEY (`charge_type_id`) 
);

CREATE TABLE `charge_type_tax_list` ( 
  `tax_type_id` int(11) NOT NULL, 
  `charge_type_id` int(11) NOT NULL, 
  PRIMARY KEY (`tax_type_id`,`charge_type_id`) 
);

CREATE TABLE `tax_type` ( 
  `tax_type_id` int(11) NOT NULL AUTO_INCREMENT, 
  `tax_type` varchar(35) NOT NULL, 
  `percentage` decimal(5,4) NOT NULL DEFAULT '0.0000', 
  `is_deleted` tinyint(1) DEFAULT '0', 
  PRIMARY KEY (`tax_type_id`) 
);

Test Data

INSERT INTO charge ( `selling_date`, `amount`, `is_deleted`, `charge_type_id` ) 
VALUES 
("2013-12-01", 50, 0, 1), 
("2013-12-01", 20, 0, 2), 
("2013-12-02", 40, 0, 1), 
("2013-12-02", 30, 0, 3), 
("2013-12-02", 30, 1, 1), 
("2013-12-03", 10, 0, 1);

INSERT INTO charge_type ( `charge_type_id`, `charge_type`, `is_deleted` ) 
VALUES 
(1, "room charge", 0), 
(2, "snack charge", 0), 
(3, "deleted charge", 1);

INSERT INTO charge_type_tax_list ( `tax_type_id`, `charge_type_id` ) 
VALUES 
(1, 1), (2, 1), (3, 1), (1, 2), (1, 3);

INSERT INTO tax_type ( `tax_type_id`, `tax_type` , `percentage`, `is_deleted` ) 
VALUES 
(1, "GST", 0.05, 0), 
(2, "HRT", 0.04, 0), 
(3, "DELETED TAX", 0.10, 1);

Solution Query

SELECT
  c.selling_date AS `Selling Date`,
  CONCAT(UPPER(SUBSTRING(ct.charge_type, 1, 1)), LOWER(SUBSTRING(ct.charge_type, 2))) AS `Charge Type`,
  FORMAT(c.amount, 2) AS `Amount`,
  FORMAT(SUM(CASE WHEN tt.is_deleted = 0 THEN tt.percentage ELSE 0 END) * c.amount, 2) AS `Tax`
FROM charge c
JOIN charge_type ct ON c.charge_type_id = ct.charge_type_id
LEFT JOIN charge_type_tax_list cttl ON ct.charge_type_id = cttl.charge_type_id
LEFT JOIN tax_type tt ON cttl.tax_type_id = tt.tax_type_id
WHERE
  c.is_deleted = 0
  AND ct.is_deleted = 0
GROUP BY c.charge_id, c.selling_date, ct.charge_type, c.amount
ORDER BY c.selling_date, ct.charge_type;

Breakdown of the Query

  1. Joins:

    • JOIN charge_type: Ensures we only pull in charge types that aren't deleted (filtered later in the WHERE clause) and links each charge to its corresponding type.
    • LEFT JOIN charge_type_tax_list: Preserves charge records even if they have no associated tax mappings (though our test data doesn't have this scenario).
    • LEFT JOIN tax_type: Connects tax mappings to their details, letting us exclude deleted taxes during calculation without dropping the charge itself.
  2. Filtering:

    • c.is_deleted = 0: Removes any charge records that were marked as deleted.
    • ct.is_deleted = 0: Filters out charges that belong to a deleted charge type (like the "deleted charge" entry in our test data).
  3. Formatting & Calculations:

    • CONCAT(...): Capitalizes the first letter of the charge type to match the sample output (e.g., "room charge" becomes "Room Charge").
    • FORMAT(...): Ensures amounts and taxes are displayed with two decimal places for consistency.
    • SUM(CASE WHEN tt.is_deleted = 0 THEN tt.percentage ELSE 0 END) * c.amount: Sums only active tax percentages, multiplies by the charge amount to get total tax. If all taxes for a charge type are deleted, this returns 0.00.
  4. Grouping: Groups by individual charge records to ensure each charge's tax is calculated correctly, even if multiple taxes apply to the same charge type.

When executed, this query produces exactly the output you requested:

Selling Date Charge Type  Amount  Tax
2013-12-01   Room Charge  50.00   4.50
2013-12-01   Snack Charge 20.00   1.00
2013-12-02   Room Charge  40.00   3.60
2013-12-03   Room Charge  10.00   0.90

内容的提问来源于stack exchange,提问作者megha saurabh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:01:28