How to Remove Dups rows from a Table in SQL Server, using a CTE statement.

Опубликовано: 04 Октябрь 2024
на канале: Software Nuggets
303
6

Search for Duplicate records, remove all duplicates.

This SQL statement uses a "Common Table Expression" (CTE) called "cteDups" to identify duplicate records in a temporary table called "#temp_production". A temporary table is a table that exists only for the duration of the current session or connection.

The statement uses the "SELECT" statement to retrieve all the records in "#temp_production". It also uses the "ROW_NUMBER" function to assign a unique number to each row in the table based on the values in the "cid" and "pid" columns. The "PARTITION BY" clause is used to group the rows by the values in the "cid" and "pid" columns, while the "ORDER BY" clause is used to sort the rows in each group.

The result of this SQL statement is a table that contains all the records from "#temp_production", along with an additional column called "rn" that contains the row numbers. The "rn" column allows the statement to identify and remove duplicate records from the table. This SQL code can be used in scenarios where data needs to be de-duplicated or cleaned up, and it can be helpful to share with other SQL developers who work with similar data cleaning tasks.

SQL remove duplicate rows
SQL delete dups using CTE
SQL del dups records
SQL Server CTE statement to delete duplicate records

‪@SoftwareNuggets‬
#softwarenuggets