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

目录

  • [1 题目](#1 题目)
  • [2 建表语句](#2 建表语句)
  • [3 题解](#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 建表语句

sql 复制代码
--建表语句
create table if not exists t_amount
(
    year    string,
    user_id string,
    value   bigint
)
    ROW FORMAT DELIMITED
        FIELDS 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正排和倒排得到两个序列

sql 复制代码
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,然后使用聚合函数取出结果。

sql 复制代码
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 rn
      from 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;
sql 复制代码
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 rn
      from t_amount) t1
group by year

执行结果

复制代码
+-------+-----------------+-----------------+
| year  | max2_user_list  | min2_user_list  |
+-------+-----------------+-----------------+
| 2022  | A,C             | B,C             |
| 2023  | B,A             | C,D             |
+-------+-----------------+-----------------+
相关推荐
20242817李臻1 分钟前
20242817李臻-安全文件传输系统-项目验收
数据库·安全
行思理13 分钟前
MongoDB慢查询临时开启方法讲解
数据库·mongodb
bbsh209924 分钟前
WebFuture 升级提示“不能同时包含聚集KEY和大字段””的处理办法
数据库·sql·mysql·webfuture
Zfox_5 小时前
Redis:Hash数据类型
服务器·数据库·redis·缓存·微服务·哈希算法
陈丹阳(滁州学院)7 小时前
若依添加添加监听容器配置(删除键,键过期)
数据库·oracle
远方16098 小时前
14-Oracle 23ai Vector Search 向量索引和混合索引-实操
数据库·ai·oracle
GUIQU.9 小时前
【Oracle】数据仓库
数据库·oracle
恰薯条的屑海鸥9 小时前
零基础在实践中学习网络安全-皮卡丘靶场(第十六期-SSRF模块)
数据库·学习·安全·web安全·渗透测试·网络安全学习
咖啡啡不加糖9 小时前
Redis大key产生、排查与优化实践
java·数据库·redis·后端·缓存
曼汐 .9 小时前
数据库管理与高可用-MySQL高可用
数据库·mysql