Search
 
SCRIPT & CODE EXAMPLE
 

SQL

Query to remove duplicate rows from a table

DELETE FROM Customers WHERE ROWID(SELECT MAX (rowid) FROM Customers C WHERE CustomerNumber = C.CustomerNumber);
Comment

sql delete duplicate rows

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;
Comment

tsql remove duplicate rows

DELETE T
FROM
(
SELECT *
, DupRank = ROW_NUMBER() OVER (
              PARTITION BY key_value
              ORDER BY (SELECT NULL)
            )
FROM original_table
) AS T
WHERE DupRank > 1
Comment

sql query to delete duplicate records

--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;
Comment

PREVIOUS NEXT
Code Example
Sql :: how to make sure two tables have same exact data in sql 
Sql :: sql values that contains certain multiple ids 
Sql :: how to update the multiple rows in sql 
Sql :: showing all columns in an sqlite table 
Sql :: mysql count with two joins 
Sql :: sql requete number pair 
Sql :: MQL4 mql4 run ea on all symbols by adding to just one chart 
Sql :: edit shchima table in sql 
Sql :: ring MySQL Create Database 
Sql :: OFFSET consulta mysql 
Sql :: get last row sqlite 
Sql :: Aktor yang pernah terlibat di film bergenre Aksi di sql 
Sql :: sql case when exists in another table 
Sql :: mysql docker image arjun 
Sql :: mysql join only one column 
Sql :: ORACLE SUBSTRING SYNTAX 
Sql :: sql compiler 
Sql :: how to run sql query in mysql workbench 
Sql :: mysql select all columns and specific fields as 
Sql :: sql int size 
Sql :: ssms keyboard shortcuts 
Sql :: Work around for mutating problem in Oracle Triggers. Please check it out. 
Csharp :: how to restart a scene in unity 
Csharp :: c# store byte array as string 
Csharp :: c# writeline debug 
Csharp :: textmesh pro text unity 
Csharp :: unity reload scene 
Csharp :: how to set a custom size for window in monogame 
Csharp :: left moust click unity 
Csharp :: check last character of a string c# 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =