当前位置:网站首页>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 .
边栏推荐
- 组队学习! 14天鸿蒙设备开发“学练考”实战营限时免费加入!
- Lake shore M91 fast hall measuring instrument
- Netease games, radical going to sea
- kubernetes命令入门(namespaces,pods)
- B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
- web开发常用的开源框架的开源协议整理
- Go语言高级
- 从零开始学 MySQL —数据库和数据表操作
- 助力数字经济发展,夯实数字人才底座—数字人才大赛在昆成功举办
- Games202 operation 0 - environment building process & solving problems encountered
猜你喜欢

Les canaux de culture intensive s'efforcent de développer Fu Xin et Wei Shi jiajie pour organiser une conférence de formation sur les nouveaux produits

Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference

Superoptimag superconducting magnet system - SOM, Som2 series

Methods of finding various limits

Digital business cloud: from planning to implementation, how does Minmetals Group quickly build a new pattern of digital development?
![[go ~ 0 to 1] day 5 July 1 type alias, custom type, interface, package and initialization function](/img/1e/bed6a761f07c052e43b1e3b1701760.png)
[go ~ 0 to 1] day 5 July 1 type alias, custom type, interface, package and initialization function
![[live broadcast appointment] database obcp certification comprehensive upgrade open class](/img/38/1ec382d0edda83d4052868255af9ea.jpg)
[live broadcast appointment] database obcp certification comprehensive upgrade open class

任务:拒绝服务DoS

M91快速霍尔测量仪—在更短的时间内进行更好的测量

Getting started with kubernetes command (namespaces, pods)
随机推荐
Netease games, radical going to sea
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
Specification of lumiprobe reactive dye indocyanine green
下载(导出)pdf模板文件(比如:审批单),报错:Invalid nested tag *** found, expected closing tag ***
Contos 7 搭建sftp之创建用户、用户组以及删除用户
论文泛读【FiLM: Visual Reasoning with a General Conditioning Layer】
Lumiprobe cell imaging study PKH26 cell membrane labeling kit
XML语法、约束
原生js打造日程表-支持鼠标滚轮滚动选择月份-可以移植到任何框架中
Is PMP cancelled??
Go语言高级
实现一个Prometheus exporter
Love business in Little Red Book
Superoptimag superconducting magnet system - SOM, Som2 series
案例分享:QinQ基本组网配置
English语法_形容词/副词3级 -注意事项
PMP是被取消了吗??
Nacos configuration file publishing failed, please check whether the parameters are correct solution
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
[6.24-7.1] review of wonderful technical blog posts in the writing community