欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 旅游 > SQL面试题练习 —— 查询前2大和前2小用户并有序拼接

SQL面试题练习 —— 查询前2大和前2小用户并有序拼接

2024/10/24 17:21:52 来源:https://blog.csdn.net/hu_wei123/article/details/140105097  浏览:    关键词:SQL面试题练习 —— 查询前2大和前2小用户并有序拼接

目录

  • 1 题目
  • 2 建表语句
  • 3 题解

1 题目


有用户账户表,包含年份,用户id和值,请按照年份分组,取出值前两小和前两大对应的用户id,需要保持值最小和最大的用户id排首位。

样例数据

+-------+----------+--------+
| year  | user_id  | value  |
+-------+----------+--------+
| 2022  | A        | 30     |
| 2022  | B        | 10     |
| 2022  | C        | 20     |
| 2023  | A        | 40     |
| 2023  | B        | 50     |
| 2023  | C        | 20     |
| 2023  | D        | 30     |
+-------+----------+--------+

期望结果

+-------+-----------------+-----------------+
| year  | max2_user_list  | min2_user_list  |
+-------+-----------------+-----------------+
| 2022  | A,C             | B,C             |
| 2023  | B,A             | C,D             |
+-------+-----------------+-----------------+

2 建表语句


--建表语句
create table if not exists t_amount
(year    string,user_id string,value   bigint
)ROW FORMAT DELIMITEDFIELDS TERMINATED BY ','STORED AS orc;--插入数据insert into t_amount(year, user_id, value)
values ('2022', 'A', 30),('2022', 'B', 10),('2022', 'C', 20),('2023', 'A', 40),('2023', 'B', 50),('2023', 'C', 20),('2023', 'D', 30)

3 题解


(1)row_number函数根据年份分组,value正排和倒排得到两个序列

select user_id, year, value, row_number() over (partition by year order by value desc) as desc_rn, row_number() over (partition by year order by value)      as rn
from t_amount

执行结果

+----------+-------+--------+----------+-----+
| user_id  | year  | value  | desc_rn  | rn  |
+----------+-------+--------+----------+-----+
| B        | 2022  | 10     | 3        | 1   |
| C        | 2022  | 20     | 2        | 2   |
| A        | 2022  | 30     | 1        | 3   |
| C        | 2023  | 20     | 4        | 1   |
| D        | 2023  | 30     | 3        | 2   |
| A        | 2023  | 40     | 2        | 3   |
| B        | 2023  | 50     | 1        | 4   |
+----------+-------+--------+----------+-----+

(2)根据年份分组,取出value最大user_id,第二大user_id,最小user_id,第二小user_id

根据年份分组,取出每年最大、第二大,最小、第二小用户ID。使用 if 对desc_rn,rn进行判断,对符合条件的数据取出 user_id,其他去null,然后使用聚合函数取出结果。

select year,max(if(desc_rn = 1, user_id, null)) as max1_user_id,max(if(desc_rn = 2, user_id, null)) as max2_user_id,max(if(rn = 1, user_id, null))      as min1_user_id,max(if(rn = 2, user_id, null))      as min2_user_id
from (select user_id, year, value, row_number() over (partition by year order by value desc) as desc_rn, row_number() over (partition by year order by value)      as rnfrom t_amount) t1
group by year

执行结果

+-------+---------------+---------------+---------------+---------------+
| year  | max1_user_id  | max2_user_id  | min1_user_id  | min2_user_id  |
+-------+---------------+---------------+---------------+---------------+
| 2022  | A             | C             | B             | C             |
| 2023  | B             | A             | C             | D             |
+-------+---------------+---------------+---------------+---------------+

(3)按照顺序拼接,得到最终结果

按照题目要求,进行字符拼接

  • 拼接max1_user_id、max2_user_id为max2_list;
  • 拼接min1_user_id、min2_user_id为min2_list;
select year,concat(max(if(desc_rn = 1, user_id, null)), ',',max(if(desc_rn = 2, user_id, null))) as max2_user_list,concat(max(if(rn = 1, user_id, null)), ',',max(if(rn = 2, user_id, null)))      as min2_user_list
from (select user_id, year, value, row_number() over (partition by year order by value desc) as desc_rn, row_number() over (partition by year order by value)      as rnfrom t_amount) t1
group by year

执行结果

+-------+-----------------+-----------------+
| year  | max2_user_list  | min2_user_list  |
+-------+-----------------+-----------------+
| 2022  | A,C             | B,C             |
| 2023  | B,A             | C,D             |
+-------+-----------------+-----------------+

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com