PRAKASH CHANDRA GUPTA PRAKASH CHANDRA GUPTA - 5 months ago 8
SQL Question

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. 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. I want the do this using a SQL script.

I have found many things to delete duplicate rows but not found solution to do it according to value of a column.

Answer

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.