WITH cte AS (
SELECT
contact_id,
first_name,
last_name,
email,
ROW_NUMBER() OVER (
PARTITION BY
first_name,
last_name,
email
ORDER BY
first_name,
last_name,
email
) row_num
FROM
sales.contacts
)
DELETE FROM cte
WHERE row_num > 1;
/*
Code language: SQL (Structured Query Language) (sql)
In this statement:
First, the CTE uses the ROW_NUMBER() function to find the duplicate rows
specified by values in the first_name, last_name, and email columns.
Then, the DELETE statement deletes all the duplicate rows but keeps only one
occurrence of each duplicate group.*/
WITH cte AS (
SELECT
contact_id,
first_name,
last_name,
email,
ROW_NUMBER() OVER (
PARTITION BY
first_name,
last_name,
email
ORDER BY
first_name,
last_name,
email
) row_num
FROM
sales.contacts
)
DELETE FROM cte
WHERE row_num > 1;
DELETE FROM [SampleDB].[dbo].[Employee]
WHERE ID NOT IN
(
SELECT MAX(ID) AS MaxRecordID
FROM [SampleDB].[dbo].[Employee]
GROUP BY [FirstName],
[LastName],
[Country]
);
DELETE DUPLICATE VALUES FROM A TABLE IN SQL SERVER
WITH cte AS (
SELECT
contact_id,
first_name,
last_name,
email,
ROW_NUMBER() OVER (
PARTITION BY
first_name,
last_name,
email
ORDER BY
first_name,
last_name,
email
) row_num
FROM
sales.contacts
)
DELETE FROM cte
WHERE row_num > 1;
Code language: SQL (Structured Query Language) (sql)
# Step 1: Copy distinct values to temporary table
CREATE TEMPORARY TABLE tmp_user (
SELECT id, name
FROM user
GROUP BY name
);
# Step 2: Remove all rows from original table
DELETE FROM user;
# Step 3: Remove all rows from original table
INSERT INTO user (SELECT * FROM tmp_user);
# Step 4: Remove temporary table
DROP TABLE tmp_user;
--ID should be primary key
--get duplicate records using RANK
SELECT E.ID,
E.firstname,
E.lastname,
E.country,
T.rank
FROM [SampleDB].[dbo].[Employee] E
INNER JOIN
(
SELECT *,
RANK() OVER(PARTITION BY firstname,
lastname,
country
ORDER BY id) rank
FROM [SampleDB].[dbo].[Employee]
) T ON E.ID = t.ID;
--delete duplications
DELETE E
FROM [SampleDB].[dbo].[Employee] E
INNER JOIN
(
SELECT *,
RANK() OVER(PARTITION BY firstname,
lastname,
country
ORDER BY id) rank
FROM [SampleDB].[dbo].[Employee]
) T ON E.ID = t.ID
WHERE rank > 1;
SELECT s.siteName, s.siteIP, h.date
FROM sites s INNER JOIN
(select h.*, row_number() over (partition by siteName order by date desc) as seqnum
from history h
) h
ON s.siteName = h.siteName and seqnum = 1
ORDER BY s.siteName, h.date
WITH CTE AS(
SELECT [col1]
,[col2]
,[col3]
,
RN = ROW_NUMBER()OVER(PARTITION BY [col1],[col2],[col3] ORDER BY [col1],[col2],[col3])
FROM [dbo].[table1]
)
DELETE FROM CTE WHERE RN > 1