I'm using SQL Server 2014 and utilizing the AdventureWorks2012
sample database provided by Microsoft.
I'm trying to delete duplicate rows using sub-query below (option #2):
/* Option #2: SUBQUERY */
--SELECT * FROM
DELETE SQLPractice.[dbo].[CURRENCY]
WHERE EXISTS (SELECT *
FROM
(SELECT
NAME,
ROW_NUMBER () OVER (PARTITION BY NAME ORDER BY NAME) AS Flag
FROM
SQLPractice.[dbo].[CURRENCY]) AS T
WHERE Flag > 1)
GO
But it deletes all rows from the table.
But the other option (CTE) does delete only the duplicate rows.
/*** Option #3: CTE ***/
;WITH RepFlag AS
(
SELECT
NAME,
ROW_NUMBER () OVER (PARTITION BY NAME ORDER BY NAME) AS Flag
FROM
SQLPractice.[dbo].[CURRENCY]
)
--SELECT * FROM RepFlag
DELETE RepFlag
WHERE Flag > 1
SELECT *
FROM SQLPractice.[dbo].[CURRENCY]
Please use the code below to create your own test table.
/*** REMOVING DUPLICATE ROWS OPTION ***/
-- Creating a table
SELECT TOP 0 *
INTO [dbo].[CURRENCY]
FROM AdventureWorks2012.Sales.Currency
WHERE NAME LIKE '%A';
-- inserting duplicate rows
INSERT [dbo].[CURRENCY]
SELECT * FROM AdventureWorks2012.Sales.Currency
WHERE NAME LIKE '%A';
/***** SELECTING COUNT OF DUPLICATED ROWS *****/
/*** Option #1: "GROUP BY" with "HAVING" ***/
SELECT
NAME, COUNT(*) AS Qty
FROM
SQLPractice.[dbo].[CURRENCY]
GROUP BY
NAME
HAVING
COUNT(*) >1
GO