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             |
+-------+-----------------+-----------------+
相关推荐
RestCloud11 小时前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud11 小时前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence13 小时前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger20 小时前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥1 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud1 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug2 天前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom2 天前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试