MySQL高阶1831-每天的最大交易

题目

编写一个解决方案,报告每天交易金额 amount 最大 的交易 ID 。如果一天中有多个这样的交易,返回这些交易的 ID 。

返回结果根据 transaction_id 升序排列

准备数据

sql 复制代码
Create table If Not Exists Transactions (transaction_id int, day date, amount int);
    Truncate table Transactions;
    insert into Transactions (transaction_id, day, amount) values ('8', '2021-4-3 15:57:28', '57');
    insert into Transactions (transaction_id, day, amount) values ('9', '2021-4-28 08:47:25', '21');
    insert into Transactions (transaction_id, day, amount) values ('1', '2021-4-29 13:28:30', '58');
    insert into Transactions (transaction_id, day, amount) values ('5', '2021-4-28 16:39:59', '40');
    insert into Transactions (transaction_id, day, amount) values ('6', '2021-4-29 23:39:28', '58');

分析数据

第一步:使用开窗函数根据amount降序

sql 复制代码
select *,
       rank() over(partition by day order by amount desc) rn
       from transactions;

第二步:选出最大交易金额的交易ID

sql 复制代码
with t1 as (
    select *,
           rank() over(partition by day order by amount desc) rn
    from transactions
)select transaction_id from t1
where rn =1
order by transaction_id asc;
相关推荐
iAm_Ike4 小时前
Go 中自定义类型与基础类型间的显式类型转换详解
jvm·数据库·python
iuvtsrt4 小时前
Golang怎么实现方法集与接口的匹配_Golang如何理解值类型和指针类型实现接口的区别【详解】
jvm·数据库·python
tongluowan0075 小时前
MySQL中列数量及长度
数据库·mysql
-liming-5 小时前
单片机设计_串口调试工具
数据库·单片机·mongodb
鹿角片ljp5 小时前
从告警检测到智能研判:SQL 注入研判模型的设计与实践
数据库·sql
小新同学^O^7 小时前
简单学习 --> Spring事务
数据库·学习·spring
前进的李工7 小时前
MySQL慢查询日志优化实战
数据库·mysql·性能优化
KaMeidebaby7 小时前
卡梅德生物技术快报|禽类成纤维细胞 FISH 实验:鸟类性别染色体基因定位技术实现与数据验证
前端·数据库·其他·百度·新浪微博
ECT-OS-JiuHuaShan7 小时前
彻底定理化:从量子纠缠到量子代谢
数据库·人工智能·学习·算法·生活·量子计算