当前位置:网站首页>Summary of SQL query de duplication statistics methods
Summary of SQL query de duplication statistics methods
2022-07-01 19:30:00 【Infinitesimal pawn】
Speaking of sql When you go to a major home, you should think of distinct This keyword . But sometimes distinct Not suitable for some special scenes .
Examples of scenes :
The following figure shows my file storage table . Now there is a need to count the number of users who upload files .
If you use distinct The number of queries is shown in the figure below . The number is 8 Obviously not , The correct quantity should be 4, Repetitive user_id It shouldn't be counted for me .
distinct What if you can't meet this situation , Another common method is to use group by Group statistics . Please look directly at the code, which I wrote SELECT COUNT(1) AS countUser FROM (SELECT COUNT(1) FROM sys_file_info GROUP BY user_id ) AS a. This sentence is correct .
Thinking of grouping statistics , Let's first look at the grouping statistics . according to user_id Divide up 4 Group , The number of each group is 1,2,1,4. Then the number of groups is the number of users after de duplication , namely 4 Group =4 Users .
Let's count the number of groups at this time , How can we count the number of groups . It is very simple to use sub query , Take the table queried by grouping as a pseudo table and make statistics again .
SELECT COUNT(1) AS countUser FROM (SELECT COUNT(1) FROM sys_file_info GROUP BY user_id ) AS a The red part is to form a pseudo table . The blue part is to count the number of entries in the pseudo table , Then we get the number of groups , That is, the number of users .
边栏推荐
- Dom4j parsing XML, XPath retrieving XML
- MySQL常用图形管理工具 | 黑马程序员
- Supervarimag superconducting magnet system SVM series
- 混沌工程平台 ChaosBlade-Box 新版重磅发布
- Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
- 【org.slf4j.Logger中info()方法】
- AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
- 论文泛读【FiLM: Visual Reasoning with a General Conditioning Layer】
- Manufacturing SRM management system supplier all-round closed-loop management, to achieve procurement sourcing and process efficient collaboration
- C-end dream is difficult to achieve. What does iFLYTEK rely on to support the goal of 1billion users?
猜你喜欢
线程的并行、并发、生命周期
使用环信提供的uni-app Demo,快速实现一对一单聊
Supervarimag superconducting magnet system SVM series
Manufacturing SRM management system supplier all-round closed-loop management, to achieve procurement sourcing and process efficient collaboration
The former 4A executives engaged in agent operation and won an IPO
求各种极限的方法
Cdga | if you are engaged in the communication industry, you should get a data management certificate
Witness the times! "The future of Renji collaboration has come" 2022 Hongji ecological partnership conference opens live broadcast reservation
【pytorch记录】自动混合精度训练 torch.cuda.amp
Games202 operation 0 - environment building process & solving problems encountered
随机推荐
Lake Shore—CRX-EM-HF 型低温探针台
Methods of finding various limits
Lumiprobe phosphide hexaethylene phosphide specification
案例分享:QinQ基本组网配置
[live broadcast appointment] database obcp certification comprehensive upgrade open class
SuperOptiMag 超导磁体系统 — SOM、SOM2 系列
AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
一次SQL优化,数据库查询速度提升 60 倍
【直播预约】数据库OBCP认证全面升级公开课
[6.24-7.1] review of wonderful technical blog posts in the writing community
小红书上的爱情买卖
论文阅读【Discriminative Latent Semantic Graph for Video Captioning】
SuperVariMag 超导磁体系统 — SVM 系列
数商云:从规划到落地,五矿集团如何快速构建数字化发展新格局?
More information about M91 fast hall measuring instrument
Love business in Little Red Book
Lake Shore continuous flow cryostat transmission line
English grammar_ Adjective / adverb Level 3 - precautions
Cdga | if you are engaged in the communication industry, you should get a data management certificate
Supervarimag superconducting magnet system SVM series