Search
 
SCRIPT & CODE EXAMPLE
 

SQL

remove duplicates sql server select

SELECT DISTINCT YourRow FROM table;
Comment

sql remove duplicates

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.*/
Comment

SQL remove duplicate

DELETE FROM [SampleDB].[dbo].[Employee]
    WHERE ID NOT IN
    (
        SELECT MAX(ID) AS MaxRecordID
        FROM [SampleDB].[dbo].[Employee]
        GROUP BY [FirstName], 
                 [LastName], 
                 [Country]
    );
Comment

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)
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

delete duplicate sql

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
Comment

PREVIOUS NEXT
Code Example
Sql :: laravel get sql query eloquent with parameters 
Sql :: postgres foreign key multiple columns 
Sql :: ubuntu connect to mssql database 
Sql :: mysql concatenate null 
Sql :: sql server update to null 
Sql :: drop view sql 
Sql :: sql server concat string and int 
Sql :: MySQL server is running with the –secure-file-priv 
Sql :: trouver doublons sql 
Sql :: psql get table data types 
Sql :: print year of a date sql 
Sql :: set id count mysql 
Sql :: DB: in eloquent using sql 
Sql :: sql manhattan distance 
Sql :: run sql command line download for windows 10 
Sql :: sql create schema 
Sql :: mysqldump --skip-lock-tables 
Sql :: ms sql print from new line 
Sql :: sql get month from date 
Sql :: pl/sql procedure example 
Sql :: sql server select first day of previous year 
Sql :: set open file limit mac catalina mysql 
Sql :: postgre describe table 
Sql :: sql change a colum to unique 
Sql :: how to select one row in mysql 
Sql :: CONVERT time string into 12 hours in sql 
Sql :: psql create user 
Sql :: sql update statement 
Sql :: oracle drop sequence 
Sql :: difference between join vs union 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =