题目描述

(通过次数2,156 | 提交次数3,504,通过率61.53%)

表: Accounts
+-------------+------+
| 列名 | 类型 |
+-------------+------+
| account_id | int |
| income | int |
+-------------+------+
account_id是这个表的主键。
每一行都包含一个银行帐户的月收入的信息。
写出一个SQL查询,来报告每个工资类别的银行账户数量。工资类别如下:
"Low Salary":所有工资 严格低于 20000 美元。
"Average Salary":包含 范围内的所有工资[$20000,$50000] 。
"High Salary":所有工资 严格大于 50000 美元。
结果表 必须 包含所有三个类别。如果某个类别中没有帐户,则报告0
按 任意顺序 返回结果表。
查询结果格式如下示例。
示例 1
输入:
Accounts 表:
+------------+--------+
| account_id | income |
+------------+--------+
| 3 | 108939 |
| 2 | 12747 |
| 8 | 87709 |
| 6 | 91796 |
+------------+--------+
输出:
+----------------+----------------+
| category | accounts_count |
+----------------+----------------+
| Low Salary | 1 |
| Average Salary | 0 |
| High Salary | 3 |
+----------------+----------------+
解释:
低薪: 数量为 2.
中等薪水: 没有.
高薪: 有三个账户,他们是 3, 68.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/count-salary-categories
表: Accounts +-------------+------+ | 列名 | 类型 | +-------------+------+ | account_id | int | | income | int | +-------------+------+ account_id是这个表的主键。 每一行都包含一个银行帐户的月收入的信息。 写出一个SQL查询,来报告每个工资类别的银行账户数量。工资类别如下: "Low Salary":所有工资 严格低于 20000 美元。 "Average Salary":包含 范围内的所有工资[$20000,$50000] 。 "High Salary":所有工资 严格大于 50000 美元。 结果表 必须 包含所有三个类别。如果某个类别中没有帐户,则报告0 。 按 任意顺序 返回结果表。 查询结果格式如下示例。 示例 1: 输入: Accounts 表: +------------+--------+ | account_id | income | +------------+--------+ | 3 | 108939 | | 2 | 12747 | | 8 | 87709 | | 6 | 91796 | +------------+--------+ 输出: +----------------+----------------+ | category | accounts_count | +----------------+----------------+ | Low Salary | 1 | | Average Salary | 0 | | High Salary | 3 | +----------------+----------------+ 解释: 低薪: 数量为 2. 中等薪水: 没有. 高薪: 有三个账户,他们是 3, 6和 8. 来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/count-salary-categories
表: Accounts
+-------------+------+
| 列名        | 类型  |
+-------------+------+
| account_id  | int  |
| income      | int  |
+-------------+------+
account_id是这个表的主键。
每一行都包含一个银行帐户的月收入的信息。

写出一个SQL查询,来报告每个工资类别的银行账户数量。工资类别如下:
"Low Salary":所有工资 严格低于 20000 美元。
"Average Salary":包含 范围内的所有工资[$20000,$50000] 。
"High Salary":所有工资 严格大于 50000 美元。
结果表 必须 包含所有三个类别。如果某个类别中没有帐户,则报告0 。
按 任意顺序 返回结果表。

查询结果格式如下示例。
示例 1:
输入:
Accounts 表:
+------------+--------+
| account_id | income |
+------------+--------+
| 3          | 108939 |
| 2          | 12747  |
| 8          | 87709  |
| 6          | 91796  |
+------------+--------+
输出:
+----------------+----------------+
| category       | accounts_count |
+----------------+----------------+
| Low Salary     | 1              |
| Average Salary | 0              |
| High Salary    | 3              |
+----------------+----------------+
解释:
低薪: 数量为 2.
中等薪水: 没有.
高薪: 有三个账户,他们是 3, 6和 8.

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/count-salary-categories
//测试数据
Create table If Not Exists Accounts (account_id int, income int);
insert into Accounts (account_id, income) values ('3', '108939');
insert into Accounts (account_id, income) values ('2', '12747');
insert into Accounts (account_id, income) values ('8', '87709');
insert into Accounts (account_id, income) values ('6', '91796');
//测试数据 Create table If Not Exists Accounts (account_id int, income int); insert into Accounts (account_id, income) values ('3', '108939'); insert into Accounts (account_id, income) values ('2', '12747'); insert into Accounts (account_id, income) values ('8', '87709'); insert into Accounts (account_id, income) values ('6', '91796');
//测试数据
Create table If Not Exists Accounts (account_id int, income int);

insert into Accounts (account_id, income) values ('3', '108939');
insert into Accounts (account_id, income) values ('2', '12747');
insert into Accounts (account_id, income) values ('8', '87709');
insert into Accounts (account_id, income) values ('6', '91796');

解题思路

这是一个按收入区间分段统计的场景。
将每个账户的收入按小于20000、20000-50000、大于50000分成3段。然后分别统计每段的账户数。
最简单的方法,就是根据3个分段的条件,将Accounts表拆分成3部分,分别统计数据量,最后UNION ALL合并到一起。
但这种方法需要对Accounts表查询3次,效率上并不是非常好。
所以,可以改为使用CASE WHEN将收入分成3段,然后根据分段分组统计账户数。
不过这个方法有个缺陷:如果某个分段一个账户都没有,则统计的结果中不会出现该分段。所以需要先构造出一个包含所有分段的临时表,再与分段结果左关联,这样可以保证每个分段都能出现在返回结果中。

参考SQL

未特别说明的情况下,参考SQL为基于MySQL8.0实现。
select 'Low Salary' category,count(1) accounts_count from Accounts where income < 20000
union all
select 'Average Salary' category,count(1) accounts_count from Accounts where income >= 20000 and income <= 50000
union all
select 'High Salary' category,count(1) accounts_count from Accounts where income > 50000;
select 'Low Salary' category,count(1) accounts_count from Accounts where income < 20000 union all select 'Average Salary' category,count(1) accounts_count from Accounts where income >= 20000 and income <= 50000 union all select 'High Salary' category,count(1) accounts_count from Accounts where income > 50000;
select 'Low Salary' category,count(1) accounts_count from Accounts where income < 20000
union all
select 'Average Salary' category,count(1) accounts_count from Accounts where income >= 20000 and income <= 50000
union all
select 'High Salary' category,count(1) accounts_count from Accounts where income > 50000;
picture loss