Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete duplicate lines in SQL Server

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about how to delete duplicate lines in SQL Server. Many people may not know much about it. In order to let everyone know more, Xiaobian summarizes the following contents for everyone. I hope you can gain something according to this article.

1. Delect table where id not in ( select max(id) from table group by col1, col2, col3... )

The field following the group by clause is the condition you use to determine duplication. For example, if there is only col1, then as long as the contents of the col1 field are the same, it means that the records are the same. 2. If yes, all fields can also be copied like this: select * into #aa from table group by id1, id2,... delete table insert into table select * from #aa

3. The copy code for the case without ID is as follows: select identity(int,1,1) as id,* into #temp from tabel select # where id not in ( select max(id) from # group by col1, col2, col3...) delect table inset into table(...) select ..... from #temp

4. col1+','+col2+','... col5 The union primary key copy code is as follows: select * from table where col1+','+col2+','... col5 in ( select max(col1+','+col2+','... col5) from table where having count(*)>1 group by col1,col2,col3,col4 )

The field following the group by clause is the condition you use to determine duplication. For example, if there is only col1, then as long as the contents of the col1 field are the same, it means that the records are the same. 5. The copy code is as follows: select identity(int,1,1) as id,* into #temp from tabel select * from #temp where id in ( select max(id) from #emp where having count(*)>1 group by col1, col2, col3...)

6. The copy code is as follows: select distinct * into #temp from tablename delete tablename go insert tablename select * from #temp Sqlclub go drop table #temp

After reading the above, do you have any further understanding of how to delete duplicate rows in SQL Server? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report