当前位置:网站首页>小白学习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会不会受到“高水位”的影响?》
近期更新的文章:
《充电宝的玄机》
近期的热文:
文章分类和索引:
边栏推荐
- ES6 数组方法
- 分布式缓存之Memcached
- Week 12 experiment -- implementation of VGA protocol based on FPGA
- Evaluation of IP location query interface Ⅱ
- (JS) imitate the indexof method to find the position of a character in the string
- Good news | Haitai Fangyuan has passed the cmmi-3 qualification certification, and its R & D capability has been internationally recognized
- Opencv4.0 installation of raspberry pie for Xiaobai
- (JS) array methods: slice and slice
- Uber前安全主管面临欺诈指控 曾隐瞒数据泄露事件
- Doodle cloud development demo login
猜你喜欢

Adding sharding sphere5.0.0 sub tables to the ruoyi framework (adding custom sub table policies through SPI)

Qt学习02 GUI程序实例分析

Qt学习07 Qt中的坐标系统

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

XML external entity injection vulnerability (I)

Data analysis method and Thinking: funnel analysis

How to test the performance of container platform, including stability, expansion efficiency and component performance

Qt学习09 计算器界面代码重构

Nature | biosynthetic potential of global marine microbiome

美创入选信通院“业务安全推进计划”首批成员单位
随机推荐
Nature | biosynthetic potential of global marine microbiome
Pipeline aggregations pipeline aggregations - parent-2
安全 创新 实践|海泰方圆受邀参加“数字时代的网信创新与价值共创”技术交流研讨会
(JS) array flat
【每日3题(3)】重新格式化电话号码
深入理解 volatile 关键字
The use of Fibonacci sequence and bubble sort in C language
[untitled] I am plotting a big event
影响LED封装散热主要因素有哪些?
Design and implementation of IDS
How to find out the wrong mobile number when querying MySQL
Object 类——万类之父
稳定币风险状况:USDT 和 USDC 安全吗?
(JS) catch errors (exceptions)
Bs-gx-018 student examination system based on SSM
Exclusive interview with head of suss NIFT: the future of Web3 is inseparable from the governance of "everyone for me, I for everyone"
Qt学习11 Qt 中的字符串类
(JS) handwriting depth comparison
STM32看门狗研究
Micro blog comment architecture design