当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
【LeetCode】118.杨辉三角
Redis Sentinel原理
Data Middle Office Construction (6): Data System Construction
逆置问题--重点
项目管理工具之燃尽图:动态考核团队工作能力
(C language) program environment and preprocessing
前序、后序及层次遍历实现二叉树的序列化与反序列化
What is the encoding that starts with ?
Mybaits Frequently Asked Questions Explained
第二十四课、二十五课,高级光照(blinn),Gamma矫正
Redis的简单使用
WEB核心【记录网站登录人数,记录用户名案例】Cookie技术实现
Day113.尚医通:用户认证、阿里云OSS、就诊人管理
Burndown chart of project management tools: Dynamic assessment of team work ability
实现线程池
loadrunner-controller-目标场景Schedule配置
我们能做出来数据库吗?
Open Kylin openKylin automation developer platform officially released
Add a shuffling effect to every pie
SQLite3交叉编译