当前位置:网站首页>Xiaobai learns MySQL - incremental statistical SQL requirements - windowing function scheme
Xiaobai learns MySQL - incremental statistical SQL requirements - windowing function scheme
2022-06-29 13:37:00 【bisal(Chen Liu)】
《 Xiaobai studies MySQL - Incremental Statistics SQL The needs of 》 in , We mentioned a MySQL Incremental statistical requirements SQL, In fact, it is not just the scheme used in the text , There will be others , Many friends mentioned that you can use MySQL 8.0 Supported windowing functions to solve .
Oracle Window function is supported in ,MySQL It's from 8.0 Start supported , Official documents ,
https://dev.mysql.com/doc/refman/8.0/en/window-functions.html
The role of the windowing function , Data before aggregation can be displayed , It can also display the aggregated data .
Review the original test data , The test table tt There are three fields ,code Is the identification name ,cdate Is the corresponding date ,ctotal It's a statistic ,

If you use the windowing function directly ,
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;Although according to cdate It's done partition by, But because of cdate The storage is not only to " month ", No clustering , So even if according to "%Y-%m", It shows several ,

You can remove the weight , Meet our needs ,
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; With the help of row_number() function , Take out the only record of each month , And then by executing partition by, Incremental accumulation is realized ,

Write according to your needs SQL, There are often many ways to write , Although the same path leads to the same goal , But if we consider the performance 、 concise 、 Easy to understand and other factors , Will be very different . If you have a better solution , Welcome private message , Learn from others .
Xiaobai studies MySQL
《 Xiaobai studies MySQL - The statistical " Be opportunistic "》
《 Xiaobai studies MySQL - Incremental Statistics SQL The needs of 》
《 Xiaobai studies MySQL - You've encountered this kind of scenario where you can't log in ?》
《 Xiaobai studies MySQL - There are some differences between users created by different versions 》
《 Xiaobai studies MySQL - A tool for randomly inserting test data 》
《 Xiaobai studies MySQL - varchar Why are type fields often defined as 255?》
《 Xiaobai studies MySQL - A case of flexible index creation 》
《 Xiaobai studies MySQL - “ Be opportunistic ” The number of records in a statistical table 》
《 Xiaobai studies MySQL - Once slow SQL The positioning of 》
《 Xiaobai studies MySQL - Talk about the importance of data backup 》
《 Xiaobai studies MySQL - InnoDB Support optimize table?》
《 Xiaobai studies MySQL - table_open_cache The role of 》
《 Xiaobai studies MySQL - Table space defragmentation method 》
《 Xiaobai studies MySQL - Case sensitive problem solving 》
《 Xiaobai studies MySQL - only_full_group_by Validation rules for 》
《 Xiaobai studies MySQL - max_allowed_packet》
《 Xiaobai studies MySQL - mysqldump Parameter differences to ensure data consistency 》
《 Xiaobai studies MySQL - The query will lock the table ?》
《 Xiaobai studies MySQL - The problem of index key length limitation 》
《 Xiaobai studies MySQL - MySQL Will be affected by “ High water level ” Influence ?》
《 Xiaobai studies MySQL - Database software and initialization installation 》
《 Xiaobai studies MySQL - Chat 》
Recently updated articles :
《 List of domestic databases 》
《 Xiaobai studies MySQL - The statistical " Be opportunistic "》
《 Xiaobai studies MySQL - Incremental Statistics SQL The needs of 》
《 Several guesses about the design of Tencent conference number 》
Recent hot articles :
《" Red Alert " Game open source code brings us a shock 》
Article classification and indexing :
边栏推荐
- Valueerror: only TF native optimizers are supported in Eagle mode
- 服务器上的RTC时间与世界时间不一致解决办法
- Acwing game 57
- C语言字符函数
- Notimplementederror: numpy() is only available when Eagle execution is enabled
- 成功解决NotImplementedError: numpy() is only available when eager execution is enabled.
- MySQL常用语句和命令汇总
- [cloud resident co creation] industrial wisdom "brain", a new acceleration script for old factories
- 云原生(三十一) | Kubernetes篇之Kubernetes平台基本预装资源
- Leetcode game 299
猜你喜欢

Cvpr2022 | panopticdepth: a unified framework for depth aware panoramic segmentation

揭秘!付费会员制下的那些小心机!

Cvpr2022 | reexamine pooling: your receptive field is not the best

Acwing 234 abandoning testing

Korean AI team plagiarizes the shock academic world! One tutor with 51 students, or plagiarism recidivist

Tutorial on building pytoch model from zero (IV) compiling training process -- Parameter Analysis
![[graduation season] it's worth it all the way over the past four years -- advice from the senior students](/img/26/d6363603ff57730970497789a05265.png)
[graduation season] it's worth it all the way over the past four years -- advice from the senior students

基于51单片机控制的BUCK开关电源Proteus仿真

Deecamp2022 officially opened! Likaifu and zhangyaqin personally teach master courses 𞓜 innovation

Write it down once Net analysis of a property management background service stuck
随机推荐
How can the sports app keep the end-to-side background alive to make the sports record more complete?
Install the terrain ovirt plug-in to provide automated management for ovirt
存算一体为何是造芯新方向?|对撞派 x 知存科技
Write it down once Net analysis of a property management background service stuck
CVPR2022 | 可精简域适应
Hundreds of CVPR people were recruited as new champions. Emoji became a "court witness". M2 MBP was exposed that the hard disk speed was reduced. Today, more big news is here
CVPR 2022 | unknown target detection module study: learning unknown targets in video
@Table爆红
Another "provincial capital university", coming!
Notimplementederror: numpy() is only available when Eagle execution is enabled
Clickhouse database uses JDBC to store milliseconds and nanoseconds
Netdata mail alarm configuration
Redis deletion policy and eviction algorithm
Autonomous and controllable city! Release of the first domestic artiq architecture quantum computing measurement and control system
C language character function
Server monitoring netdata panel configuring mail service
3个最佳实践助力企业改善供应链安全
Create an API rapid development platform, awesome!
Prometheus 2.28.0 new features
Pod security policy (PSP)