当前位置:网站首页>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 .
边栏推荐
- B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
- More information about M91 fast hall measuring instrument
- Today, with the popularity of micro services, how does service mesh exist?
- Detailed explanation of JUnit unit test framework
- CDGA|从事通信行业,那你应该考个数据管理证书
- MATLAB中subplot函数的使用
- MFC中如何重绘CListCtrl的表头
- Lake shore optimag superconducting magnet system om series
- 【pytorch记录】自动混合精度训练 torch.cuda.amp
- 【英语语法】Unit1 冠词、名词、代词和数词
猜你喜欢

数商云:从规划到落地,五矿集团如何快速构建数字化发展新格局?

C端梦难做,科大讯飞靠什么撑起10亿用户目标?

Digital business cloud: from planning to implementation, how does Minmetals Group quickly build a new pattern of digital development?

数字化转型企业成功的关键,用数据创造价值

Chaos engineering platform chaosblade box new heavy release

Lumiprobe phosphide hexaethylene phosphide specification

智慧防疫系统为建筑工地复工复产提供安全保障

M91 fast hall measuring instrument - better measurement in a shorter time

6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单

【To .NET】C#集合类源码解析
随机推荐
CDGA|从事通信行业,那你应该考个数据管理证书
赋能「新型中国企业」,SAP Process Automation 落地中国
Dom4J解析XML、Xpath检索XML
云服务器ECS夏日省钱秘籍,这次@老用户快来领走
狼人杀攻略:你当我好骗吗,我们相信谁!
线程的并行、并发、生命周期
English grammar_ Adjective / adverb Level 3 - precautions
CMU AI PhD 第一年总结
The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
物联网平台thingsboard搭建学习记录
PostgreSQL varchar[] array type operation
【pytorch记录】自动混合精度训练 torch.cuda.amp
Redis 实现限流的三种方式
Once the SQL is optimized, the database query speed is increased by 60 times
【To .NET】C#集合类源码解析
B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
Lake Shore低温恒温器的氦气传输线
Implement a Prometheus exporter
中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒
Yyds dry inventory ravendb start client API (III)