题目描述

(通过次数4,917 | 提交次数7,039,通过率69.85%)

Salaries 表:
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| company_id    | int     |
| employee_id   | int     |
| employee_name | varchar |
| salary        | int     |
+---------------+---------+
(company_id, employee_id) 是这个表的主键
这个表包括员工的company id, id, name 和 salary 

写一条查询 SQL 来查找每个员工的税后工资
每个公司的税率计算依照以下规则
如果这个公司员工最高工资不到 1000 ,税率为 0%
如果这个公司员工最高工资在 1000 到 10000 之间,税率为 24%
如果这个公司员工最高工资大于 10000 ,税率为 49%
按任意顺序返回结果,税后工资结果取整

结果表格式如下例所示:
Salaries 表:
+------------+-------------+---------------+--------+
| company_id | employee_id | employee_name | salary |
+------------+-------------+---------------+--------+
| 1          | 1           | Tony          | 2000   |
| 1          | 2           | Pronub        | 21300  |
| 1          | 3           | Tyrrox        | 10800  |
| 2          | 1           | Pam           | 300    |
| 2          | 7           | Bassem        | 450    |
| 2          | 9           | Hermione      | 700    |
| 3          | 7           | Bocaben       | 100    |
| 3          | 2           | Ognjen        | 2200   |
| 3          | 13          | Nyancat       | 3300   |
| 3          | 15          | Morninngcat   | 7777   |
+------------+-------------+---------------+--------+
Result 表:
+------------+-------------+---------------+--------+
| company_id | employee_id | employee_name | salary |
+------------+-------------+---------------+--------+
| 1          | 1           | Tony          | 1020   |
| 1          | 2           | Pronub        | 10863  |
| 1          | 3           | Tyrrox        | 5508   |
| 2          | 1           | Pam           | 300    |
| 2          | 7           | Bassem        | 450    |
| 2          | 9           | Hermione      | 700    |
| 3          | 7           | Bocaben       | 76     |
| 3          | 2           | Ognjen        | 1672   |
| 3          | 13          | Nyancat       | 2508   |
| 3          | 15          | Morninngcat   | 5911   |
+------------+-------------+---------------+--------+
对于公司 1 ,最高工资是 21300 ,其每个员工的税率为 49%
对于公司 2 ,最高工资是 700 ,其每个员工税率为 0%
对于公司 3 ,最高工资是 7777 ,其每个员工税率是 24%
税后工资计算 = 工资 - ( 税率 / 100)*工资
对于上述案例,Morninngcat 的税后工资 = 7777 - 7777 * ( 24 / 100) = 7777 - 1866.48 = 5910.52 ,取整为 5911

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/calculate-salaries
//测试数据

#测试数据
Create table If Not Exists Salaries (company_id int, employee_id int, employee_name varchar(13), salary int);

insert into Salaries (company_id, employee_id, employee_name, salary) values ('1', '1', 'Tony', '2000');
insert into Salaries (company_id, employee_id, employee_name, salary) values ('1', '2', 'Pronub', '21300');
insert into Salaries (company_id, employee_id, employee_name, salary) values ('1', '3', 'Tyrrox', '10800');
insert into Salaries (company_id, employee_id, employee_name, salary) values ('2', '1', 'Pam', '300');
insert into Salaries (company_id, employee_id, employee_name, salary) values ('2', '7', 'Bassem', '450');
insert into Salaries (company_id, employee_id, employee_name, salary) values ('2', '9', 'Hermione', '700');
insert into Salaries (company_id, employee_id, employee_name, salary) values ('3', '7', 'Bocaben', '100');
insert into Salaries (company_id, employee_id, employee_name, salary) values ('3', '2', 'Ognjen', '2200');
insert into Salaries (company_id, employee_id, employee_name, salary) values ('3', '13', 'Nyancat', '3300');
insert into Salaries (company_id, employee_id, employee_name, salary) values ('3', '15', 'Morninngcat', '7777');

解题思路

税后工资=税前工资 – 税前工资*税率。
公式中,税前工资可以直接从Salaries表获取,我们缺少的只是税率。
根据题目描述,一个公司的税率,是根据这个公司的最高工资来决定的。
那么,我们可以先计算出每个公司的最高工资,从而得出每个公司的税率。使用GROUP BY + MAX + CASE WHEN可以很简单的计算出来。
然后,再根据员工所在公司的税率,计算出每个员工的税后工资即可。
当然,题目要求对工资做四舍五入的处理,去除小数。使用ROUND函数可以实现。

参考SQL

未特别说明的情况下,参考SQL为基于MySQL8.0实现。
with
tmp as (
    select
        company_id,
        case when max(salary) < 1000 then 0
             when max(salary) between 1000 and 10000 then 24
             when max(salary) > 10000 then 49 end tax_rate
    from Salaries
    group by company_id
)
select
    a.company_id,
    a.employee_id,
    a.employee_name,
    round(a.salary * (100-tax_rate) / 100,0) salary
from Salaries a
inner join tmp b
on a.company_id = b.company_id;
picture loss