当前位置:网站首页>小白学习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会不会受到“高水位”的影响?》
近期更新的文章:
《充电宝的玄机》
近期的热文:
文章分类和索引:
边栏推荐
- 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
- 巴比特 | 元宇宙每日必读:HTC 宣布推出首款元宇宙手机,售价约2700元人民币,都有哪些新玩法?...
- Pipeline aggregations管道聚合- parent-2
- X-FRAME-OPTIONS web page hijacking vulnerability
- QT learning 09 calculator interface code reconstruction
- Evaluation of IP location query interface Ⅱ
- Object 类——万类之父
- Nature | biosynthetic potential of global marine microbiome
- [daily 3 questions (1)] judge the color of a grid on the chess board
- 关于IP定位查询接口的测评Ⅱ
猜你喜欢

Rebuild confidence in China's scientific research - the latest nature index 2022 released that China's research output increased the most

巴比特 | 元宇宙每日必读:HTC 宣布推出首款元宇宙手机,售价约2700元人民币,都有哪些新玩法?...

Introduction to software engineering - Chapter 5 - overall design

(JS) filter out keys with value greater than 2 in the object

ruoyi框架中添加sharding sphere5.0.0分表(通过spi添加自定义分表策略)

Oracle NetSuite 助力 TCM Bio,洞悉数据变化,让业务发展更灵活

Pipeline aggregations pipeline aggregations - parent-2

行业分析| 快对讲,楼宇对讲

中国计算语言学大会、全国知识图谱与语义计算大会赛题火热进行中
![LeetCode 535 TinyURL的加密与解密[map] HERODING的LeetCode之路](/img/76/709bbbbd8eb01f32683a96c4abddb9.png)
LeetCode 535 TinyURL的加密与解密[map] HERODING的LeetCode之路
随机推荐
基于鲲鹏开发套件实现x86平台到Kunpeng平台的源码迁移【玩转华为云】
Nature | 全球海洋微生物组的生物合成潜力
QT learning 11 string classes in QT
The use of Fibonacci sequence and bubble sort in C language
Micro blog comment architecture design
Is it safe to open a stock account online
【每日3题(1)】判断国际象棋棋盘中一个格子的颜色
Pipeline aggregations管道聚合-Sibling-1
又拍雲 Redis 的改進之路
Pipeline aggregations pipeline aggregations - parent-2
Self-Improvement! Junior college "counter attack" master of Zhejiang University, 3 SCI, and finally become a doctor of Tsinghua University!
【毕业季】总结过去,展望未来
(JS) handwritten bind function
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
哈希Hash竞猜游戏系统开发详解技术丨哈希竞猜游戏系统开发方案解析
Modbustcp protocol WiFi wireless learning single channel infrared module (round shell version)
Bs-gx-017 online examination management system based on SSM
Exclusive interview with CTO: the company has deepened the product layout and accelerated the technological innovation of domestic EDA
XML external entity injection vulnerability (I)
Unity learning notes --vector3 how to set default parameters