简体   繁体   English

使用列的重复值从表中删除记录,例如QuestionId

[英]Delete records from a table using aduplicate value of a column say QuestionId

Delete records from a table using aduplicate value of a column say QuestionId. 使用列的重复值“ QuestionId”从表中删除记录。 If I have two records having QuestionId 12123 (do not think about values of other columns) then I want to delete the first record having the QuestionId 12123 and leave second one as it is. 如果我有两个记录有QuestionId 12123(不要考虑其他列的值),那么我想删除第一个记录有QuestionId 12123的记录,而保留第二个记录。 I want the do this using a SQL script. 我想要使​​用SQL脚本执行此操作。

I have found many things to delete duplicate rows but not found solution to do it according to value of a column. 我发现有很多事情可以删除重复的行,但找不到根据列的值执行此操作的解决方案。

To Delete Duplicate records from a table for any column: 要从表中删除任何列的重复记录:

WITH CTE (AspNetId,MemberId, DuplicateCount)
AS
(
SELECT AspNetId,MemberId,
ROW_NUMBER() OVER(PARTITION BY AspNetId ORDER BY AspNetId) AS DuplicateCount
FROM [dbo].[apta_Profile]
)
DELETE
FROM CTE
WHERE DuplicateCount > 1
GO

I am not sure what you are referring with Foreign key. 我不确定您使用外键指的是什么。 Please elaborate. 请详细说明。

A comma was missing from the Script so I was getting error while Execution so I have corrected it now. 脚本中缺少逗号,因此执行时出现错误,因此,现在已更正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM