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

sql delete duplicate rows but keep one

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

sql script to delete duplicate records in a table

WITH CTE([FirstName], 
    [LastName], 
    [Country], 
    DuplicateCount)
AS (SELECT [FirstName], 
           [LastName], 
           [Country], 
           ROW_NUMBER() OVER(PARTITION BY [FirstName], 
                                          [LastName], 
                                          [Country]
           ORDER BY ID) AS DuplicateCount
    FROM [SampleDB].[dbo].[Employee])
DELETE FROM CTE
WHERE DuplicateCount > 1;
/*will delete the duplicate value*/
Comment

PREVIOUS NEXT
Code Example
Sql :: before delete trigger mysql 
Sql :: run sql command line download 
Sql :: get month and year from date in mysql sequelize 
Sql :: like and not like together in sql 
Sql :: why do we need data structure in sql 
Sql :: tsql utf to local time 
Sql :: python connect to mysql in settings.py 
Sql :: guid string to binary better 
Sql :: luu ckeditor vao mysql 
Csharp :: create a directory if it doesnt exist c# 
Csharp :: vb.net yes no cancel 
Csharp :: hello world program in c# 
Csharp :: change border color of textfield in flutter 
Csharp :: center an image horizontally and vertically 
Csharp :: c# get pc ip address 
Csharp :: asp.net c# write string to text file 
Csharp :: import C++ into C# 
Csharp :: c# check if a directory exists 
Csharp :: blazor get current url 
Csharp :: for loop unity 
Csharp :: stop audio unity 
Csharp :: unity textmeshpro 
Csharp :: c# datagridview hide column 
Csharp :: Csharp cast string to double 
Csharp :: how to create directory folder in c# 
Csharp :: c# absolute value 
Csharp :: how to get random numbers in c# 
Csharp :: Unity rotate player to mouse point slowly 
Csharp :: c# run as administrator 
Csharp :: message box in visual studio 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =