Leetcode力扣 MySQL数据库 1907 按分类统计薪水

1907 按分类统计薪水

SQL架构

Create table If Not Exists Accounts_1907 (account_id int, income int);
Truncate table Accounts_1907;
insert into Accounts_1907 (account_id, income) values ('3', '108939');
insert into Accounts_1907 (account_id, income) values ('2', '12747');
insert into Accounts_1907 (account_id, income) values ('8', '87709');
insert into Accounts_1907 (account_id, income) values ('6', '91796');


表: Accounts

+-------------+------+
| 列名        | 类型  |
+-------------+------+
| account_id  | int  |
| income      | int  |
+-------------+------+
account_id 是这个表的主键。
每一行都包含一个银行帐户的月收入的信息。
写出一个 SQL 查询,来报告每个工资类别的银行账户数量。 工资类别如下:

“低薪”:所有工资严格低于20000美元。
“中等薪水”:包含范围内的所有工资 [$20000, $50000]。
“高薪”:所有工资严格大于50000美元。

结果表必须包含所有三个类别。 如果某个类别中没有帐户,则报告 0。

按任意顺序返回结果表。

查询结果格式如下示例:

Accounts 表:
+------------+--------+
| account_id | income |
+------------+--------+
| 3          | 108939 |
| 2          | 12747  |
| 8          | 87709  |
| 6          | 91796  |
+------------+--------+

Result 表:
+----------------+----------------+
| category       | accounts_count |
+----------------+----------------+
| Low Salary     | 1              |
| Average Salary | 0              |
| High Salary    | 3              |
+----------------+----------------+

低薪: 数量为 2.
中等薪水: 没有.
高薪: 有三个账户,他们是 3, 6和 8.

解题

select 'low salary' category, count(account_id) accounts_count from Accounts_1907 ac where income < 20000
union
select 'average salary' category, count(account_id) accounts_count from Accounts_1907 ac where income between 20000 and 50000
union
select 'high salary' category, count(account_id) accounts_count from Accounts_1907 ac where income > 50000;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ziko-1101

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值