当前位置:网站首页>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
边栏推荐
- &#x开头的是什么编码?
- 作为面试官,关于线程池的问题我一般这样套路...
- NowCoderTOP17-22 Binary search/sort - continuous update ing
- NowCoderTOP23-27二叉树遍历——持续更新ing
- Module eight
- 可以用聚酯树脂将接线板密封接线盒吗?(接线盒灌封胶用哪种树脂)
- loadrunner脚本--添加事务
- 医院管理系统数据库,课程设计,SQLserver,纯代码设计
- Mybaits Frequently Asked Questions Explained
- loadrunner-controller-view script与load generator
猜你喜欢
随机推荐
Day113. Shangyitong: user authentication, Alibaba Cloud OSS, patient management
第七章
DC-7-vulnhub
js滚动条滚动到指定元素
Echart饼图添加轮播效果
【LeetCode】1161.最大层内元素和
SQL学习笔记——REGEXP运算符
顺序表的删除
SQL如何从字符串截取指定字符(LEFT、MID、RIGHT三大函数)
可以用聚酯树脂将接线板密封接线盒吗?(接线盒灌封胶用哪种树脂)
NowCoderTOP17-22 二分查找/排序——持续更新ing
恋爱期间的赠与能否撤销
loadrunner-controller-目标场景Schedule配置
感情危机,朋友的网恋女友要和他闹分手,问我怎么办
Business-(Course-Chapter-Subsection) + Course Publishing Some Business Ideas
Kotlin入门介绍篇
初识二叉搜索树
小程序如何使用订阅消息(PHP代码+小程序js代码)
[NLP] Interpretation of Transformer Theory
Principle of Redis Sentinel