当前位置:网站首页>Xiaobai needs to learn MySQL - incremental statistical SQL
Xiaobai needs to learn MySQL - incremental statistical SQL
2022-06-24 09:12:00 【bisal(Chen Liu)】
This article was launched in the open source community of aikesheng 《 Technology sharing | MySQL A cluster increment statistic in SQL The needs of 》.
My colleague raised a question MySQL In the database SQL The problem of incremental statistics , I use test data to simulate , The test table tt There are three fields ,code Is the identification name ,cdate Is the corresponding date ,ctotal It's a statistic ,

The original statistical statement , according to code and cdate Clustering , Count out each code Every month ctotal Accumulated records ,

But his need is to be able to follow code+cdate The statistical value of is displayed accumulatively , For example, the first line in the above figure total yes 10, Second elements total Show 10+9=19, In the third line total Show 10+9+11=30, And so on .
What he needs is total Line by line ,MySQL Can be achieved by defining variable accumulation , utilize @i Line by line ,

But there's a problem , Different code, No zeroing , For example, in the figure above ,code=BBB The first line of the record ,total Should be 5, Now it's 35, Obviously 30+5 Got .
There should be another logic , according to code While clustering sum Sum calculation , As shown below , Now you can see ,g_total according to code and cdate Add up , Different code, It will return to zero , Recalculate ,

In fact, this one can be optimized ,MySQL 8.0 Support with, Above SQL Chinese vs tt The table was read twice , With the help of with, It can be reduced to read only once tt surface ,

in addition , Combined with the actual retrieval scenario requirements , Consider adding indexes to related fields , Further improve the efficiency of data retrieval .
Of course , The above SQL There may be other alternatives or better solutions , This article only gives one of the solutions .
therefore , adopt SQL Achieve business needs , On the one hand, we need to fully understand the meaning of requirements , Can accurately map to specific SQL logically , On the other hand, you should know the functions supported by the database 、 function 、 Characteristics, etc. , Whether there is an implementation that can meet this requirement , besides , Non functional factors , It can't be ignored , The right index 、 Avoid reading data repeatedly 、 Avoiding unnecessary sorting and so on are the means we can use , This is it. " work out measures to suit local conditions , The water is impermanent ".
Xiaobai studies MySQL
《 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 :
《 Several guesses about the design of Tencent conference number 》
《 China Super League and Guoan in the new season , Through the thorns 》
《Oracle Of CTAS Can I bring constraints and other attributes to the new table ?》
Article classification and indexing :
边栏推荐
- Sword finger offer 55 - I. depth DFS method of binary tree
- Depens:*** but it is not going to be installed
- 4275. Dijkstra序列
- 软件系统依赖关系分析
- Depens:*** but it is not going to be installed
- MBA-day25 最值问题-应用题
- Installation of sophus package in slam14 lecture
- 常用表情符号
- Linux (centos7.9) installation and deployment of MySQL Cluster 7.6
- MySQL - SQL statement
猜你喜欢
![[quantitative investment] discrete Fourier transform to calculate array period](/img/0d/aac02463ff403fb1ff871af5ff91fa.png)
[quantitative investment] discrete Fourier transform to calculate array period

浮点数表示法(总结自CS61C和CMU CSAPP)

MySQL data (Linux Environment) scheduled backup

The native applet uses canvas to make posters, which are scaled to the same scale. It is similar to the uniapp, but the writing method is a little different
![[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle](/img/db/e581087e550a2e460f12047685c48f.png)
[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle
![[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)](/img/b3/b156d75c7b4f03580c449f8499cd74.png)
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)

Every (), map (), forearch () methods. There are objects in the array
![[noi Simulation Competition] send (tree DP)](/img/5b/3beb9f5fdad00b6d5dc789e88c6e98.png)
[noi Simulation Competition] send (tree DP)

linux(centos7.9)安装部署mysql-cluster 7.6

Squid代理服务器应用
随机推荐
520. detect capital letters
【牛客】把字符串转换成整数
MySQL | view notes on Master Kong MySQL from introduction to advanced
Huawei Router: IPSec Technology
12、 Demonstration of all function realization effects
解决:jmeter5.5在win11下界面上的字特别小
【LeetCode】415. String addition
Framework tool class obtained by chance for self use
[e325: attention] VIM editing error
Data middle office: detailed explanation of technical architecture of data middle office
Threejs glow channel 01 (unrealbroompass & layers)
P6117-[JOI 2019 Final]コイン集め【贪心】
uniapp 开发多端项目如何配置环境变量以及区分环境打包
牛客网 实现简单计算器功能
tcpdump抓包实现过程
阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
Epidemic situation, unemployment, 2022, we shouted to lie down!
【Redis实现秒杀业务①】秒杀流程概述|基本业务实现
常用表情符号
Data middle office: the data middle office practice scheme of Minsheng Bank