当前位置:网站首页>小白学习MySQL - 增量统计SQL的需求 - 开窗函数的方案
小白学习MySQL - 增量统计SQL的需求 - 开窗函数的方案
2022-06-29 10:47:00 【bisal(Chen Liu)】
《小白学习MySQL - 增量统计SQL的需求》中,我们提到了一个MySQL增量统计需求的SQL,其实不止文中用的方案,还会有其他的,很多朋友都提到可以使用MySQL 8.0支持的开窗函数来解决。
Oracle中支持开窗函数,MySQL是从8.0开始支持的,官方文档,
https://dev.mysql.com/doc/refman/8.0/en/window-functions.html
开窗函数的作用,既可显示聚合前的数据,又可显示聚合后的数据。
回顾一下原始的测试数据,测试表tt有三个字段,code是标识名称,cdate是对应的日期,ctotal是个统计值,

如果直接用开窗函数,
select code, date_format(cdate, '%Y-%m'),
ctotal,
sum(ctotal)
over (partition by code, date_format(cdate, '%Y-%m')
order by date_format(cdate, '%Y-%m')) as total
from tt;虽然按照cdate做的partition by,但是由于cdate存储的不仅是到"月",没进行聚类,所以即使按照"%Y-%m",展示的是多条,

可以通过去重,达到我们的需求,
select t.code, t.cdate, t.total,
sum(t.total) over (partition by t.code order by t.cdate) as g_total
from
(select code, date_format(cdate, '%Y-%m') as cdate,
ctotal,
row_number() over (partition by code, date_format(cdate, '%Y-%m')
order by code, date_format(cdate, '%Y-%m')) as r_seq,
sum(ctotal)
over (partition by code, date_format(cdate, '%Y-%m')
order by code, date_format(cdate, '%Y-%m')) as total
from tt) t
where t.r_seq = 1;借助了row_number()函数,取出每个月的唯一一条记录,再通过执行partition by,实现了增量累加,

根据需求写SQL,往往可能有很多种写法,虽然殊途同归,但若考虑性能、简洁、易懂等因素,就会大相径庭。如果各位有更好的解决方案,欢迎私信,借鉴学习。
小白学习MySQL
《小白学习MySQL - varchar类型字段为什么经常定义成255?》
《小白学习MySQL - TIMESTAMP类型字段非空和默认值属性的影响》
《小白学习MySQL - InnoDB支持optimize table?》
《小白学习MySQL - table_open_cache的作用》
《小白学习MySQL - only_full_group_by的校验规则》
《小白学习MySQL - max_allowed_packet》
《小白学习MySQL - mysqldump保证数据一致性的参数差异》
《小白学习MySQL - MySQL会不会受到“高水位”的影响?》
近期更新的文章:
《充电宝的玄机》
近期的热文:
文章分类和索引:
边栏推荐
- Qt学习16 Qt 对象间的父子关系
- Qt学习10 初探 Qt 中的消息处理
- [various * * question series] what are OLTP and OLAP?
- 【高并发】2.8万字的Callable和Future面试知识点总结,看完我直接面进了字节跳动,原谅我有点飘了(上)
- 分布式缓存之Memcached
- (JS) array flat
- 稳定币风险状况:USDT 和 USDC 安全吗?
- ES6 数组方法
- (JS) filter out keys with value greater than 2 in the object
- ruoyi框架中添加sharding sphere5.0.0分表(通过spi添加自定义分表策略)
猜你喜欢

Babbitt | yuancosmos daily must read: HTC announced the launch of the first yuancosmos mobile phone, which costs about 2700 yuan. What are the new ways to play

Qt学习01 GUI程序原理分析

软件工程导论——第五章——总体设计

Course design for the end of the semester: product sales management system based on SSM

Modbustcp protocol WiFi wireless learning single channel infrared module (small shell version)

信息技术应用创新专业人员(数据库)中级培训火热招生中(7月6-10日)

Nature | 全球海洋微生物组的生物合成潜力

Bs-gx-018 student examination system based on SSM

新版CorelDRAW Technical Suite2022最新详细功能介绍
![[daily 3 questions (3)] reformat the phone number](/img/ba/0cfe8c084e626615934065b4dee453.png)
[daily 3 questions (3)] reformat the phone number
随机推荐
Safety innovation practice | Haitai Fangyuan was invited to participate in the technical exchange Seminar on "network information innovation and value co creation in the digital age"
通过网上进行股票开户安全吗
Data analysis method and Thinking: funnel analysis
CTO专访:合见工软深化产品布局 加速国产EDA技术革新
中国计算语言学大会、全国知识图谱与语义计算大会赛题火热进行中
(JS) handwritten bind function
After 22 years in office, the father of PowerShell will leave Microsoft: he was demoted by Microsoft for developing PowerShell
Hit the industry directly! The first model selection tool in the industry was launched by the flying propeller
QT learning 15 separation of user interface and business logic
面试高并发,凉了!!(全程高能,建议收藏)
How to identify the exact length and width of the contour
稳定币风险状况:USDT 和 USDC 安全吗?
毕业5年,我问遍了身边的大佬,总结了他们的学习方法
关于IP定位查询接口的测评Ⅱ
Qt学习09 计算器界面代码重构
Nature | biosynthetic potential of global marine microbiome
Modbustcp protocol network learning single channel infrared module (double-layer board)
专访 SUSS NiFT 负责人:Web3 的未来离不开“人人为我,我为人人”的治理
What are the pop, push, unshift, and shift of the (JS) array?
The use of variables in shell that you have to be familiar with