Tuesday, 4 August 2015

Delete duplicate entry from sqlserver table

Remove Repeted entry  from a table .


let us learn how to delete duplicate data from table .
Suppose there is a table with name “Employee” with two columns Id and Name , and there is no primary key involved in this table , hence there is possibility of having repeated values now , we have to delete those repeated values.

With empTemp as
(
Select *,row_number() over(partition by id, name order by id, name) as empTemp from employee
)
Delete from empTemp where empTemp>1


Note :

Row_number() : Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.

Over :  the OVER clause defines a window or user-specified set of rows within a query result set. A window function then computes a value for each row in the window. You can use the OVER clause with functions to compute aggregated values such as moving averages, cumulative aggregates, running totals, or a top N per group results.
With  : Specifies a temporary named result set, known as a common table expression (CTE).

No comments:

Post a Comment