Remove duplicate records except the first record in SQL(删除 SQL 中除第一条记录外的重复记录)
问题描述
我想删除除第一个之外的所有重复记录.
I want to remove all duplicate records except the first one.
喜欢:
NAME
R
R
rajesh
YOGESH
YOGESH
现在我想删除上面的第二个R"和第二个YOGESH".
Now in the above I want to remove the second "R" and the second "YOGESH".
我只有一列名称为NAME".
I have only one column whose name is "NAME".
推荐答案
使用 CTE(我有几个在生产中).
Use a CTE (I have several of these in production).
;WITH duplicateRemoval as (
SELECT
[name]
,ROW_NUMBER() OVER(PARTITION BY [name] ORDER BY [name]) ranked
from #myTable
ORDER BY name
)
DELETE
FROM duplicateRemoval
WHERE ranked > 1;
说明:CTE 将获取您的所有记录并为每个唯一条目应用一个行号.每个额外的条目将获得一个递增的数字.将 DELETE 替换为 SELECT * 以查看它的作用.
Explanation: The CTE will grab all of your records and apply a row number for each unique entry. Each additional entry will get an incrementing number. Replace the DELETE with a SELECT * in order to see what it does.
这篇关于删除 SQL 中除第一条记录外的重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:删除 SQL 中除第一条记录外的重复记录
基础教程推荐
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- oracle区分大小写的原因? 2021-01-01
- 在多列上分布任意行 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
