当前位置:网站首页>Summary of three methods for SQL deduplication
Summary of three methods for SQL deduplication
2022-07-31 10:17:00 【xiaoweiwei99】
A summary of three methods of SQL deduplication

Deduplication here means: when querying, do not display duplicates, not delete duplicates in the table
1.distinctDe-duplication
Points to note: distinct
can only de-duplicate in one column. When distinct is followed by more than 1 parameter, the relationship between them is &&(logicand) relationship, only all conditions are the same to deduplicate
Disadvantage: When there are many fields in the query, distinct will act on multiple fields, resulting in more deduplication conditions
select distinct UserResult from Table1

2.group byDe-duplication
Principle of de-duplication: group duplicate rows, and only display the first row of the same data
Disadvantage: After using group by, all queriesAll fields need to use aggregate functions, which is cumbersome
select min(UserName)UserName,min(UserSex)UserSex,min(UserSubject)UserSubject,min(UserResult)UserResult from Table1
group by UserResult

3.row_number() over (parttion by grouping column order by sorting column)
Disadvantage: Xiaomeng doesn't know yet
Deduplication principle: Now group by repeating column, and then sort after grouping, differentThe group number is 1, the same group number is 2, and the deduplication effect is achieved if the group number is 2.
select *from
(
– query duplicate rows
select *,row_number()over (partition by UserResult order by UserResult desc)num from Table1
)A
where A.num=1

Here AmwayThe third one, row_number(), be steady!
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢
随机推荐
逆置问题--重点
Mybaits 常用问题详解
SQL存储过程详解
Kotlin—基本语法 (四)
使用turtle画按钮
Solve rpc error: code = Unimplemented desc = method CheckLicense not implemented
【LeetCode】Day108-和为 K 的子数组
SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
Principle of Redis Sentinel
SQL如何从字符串截取指定字符(LEFT、MID、RIGHT三大函数)
让动画每次重复前都有延迟
cocoaPods管理之后工程结构变化
(C语言)程序环境和预处理
第六章
loadrunner脚本--添加检查点
[NLP] Interpretation of Transformer Theory
出色的移动端用户验证
湖仓一体电商项目(二):项目使用技术及版本和基础环境准备
浅谈Attention与Self-Attention,一起感受注意力之美
Dart Log tool class









