Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize grouping according to continuous equivalence in MySQL Library

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to achieve continuous equivalent grouping in the MySQL library". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the continuous equivalent grouping in the MySQL library".

This grouping is based on comparing the values of the previous line of fields and generating a new group when it changes. SQL only supports equivalent grouping. In order to realize this kind of ordered grouping, it has to be transformed into equivalent grouping after several data transformations. Take the new version of MySQL, which supports window functions, as an example, it goes through three steps:

1. Get the change flag field flag. Get the field value of the previous line through the window function lag, and change flag=1, otherwise it is 0

2. Accumulate flag fields to get acc fields. Add up from the first line to the current line, you still have to use the window function: sum (flag)... BETWEEN ROWS UNBOUNDED AND CURRENT ROW

3. Make regular equivalent grouping according to the acc field.

In the early days, MySQL without window functions can also be realized in theory, but it is more complex, so we won't go into details.

This kind of ordered grouping is very simple if you use the SPL language, and you can do it in one sentence with the @ o option of group operation:

= connect ("mysqlDB") .query ("select * from t") .group @ o (f)

After completing the grouping action, the sequence table B of the two-tier structure is obtained. For the second layer grouping subset, it is easy to do any calculation:

= B.max (~ .len ()) to get the maximum number of rows of the group

= B. (~ .new (frecom avg (f2)). Conj () to get the average value of f2 for each group.

= B. (~ .sort @ z (f2) .m (to (3)). Conj () take out the three largest lines of f2 in each group

Thank you for your reading, the above is the content of "how to achieve continuous equivalent grouping in MySQL library". After the study of this article, I believe you have a deeper understanding of how to achieve continuous equivalent grouping in MySQL library, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report