DEV Community

Burak Kepti
Burak Kepti

Posted on

How to remove duplications on TSQL

There is always a situation in which we need to remove duplications in our queries. There is an advanced way to do this for multi-purpose purpose usage. Assume that we are importing data from 3rd part source like this.

`IF OBJECT_ID(N'tempdb..#Product') IS NOT NULL
BEGIN
DROP TABLE #Product
END
create TABLE #Product (Name VARCHAR(100),Description VARCHAR(255));

INSERT INTO #Product(Name, Description) VALUES ('Entity Framework Extensions', 'Entity Framework Extensions Description.');
INSERT INTO #Product(Name, Description) VALUES ('Dapper Plus', 'Dapper Plus Description.');
INSERT INTO #Product(Name, Description) VALUES ('C# Eval Expression', 'C# Eval Description');
INSERT INTO #Product(Name, Description) VALUES ('Entity Framework Extensions', 'Entity Framework Extensions Description.');
INSERT INTO #Product(Name, Description) VALUES ('Dapper Plus', 'Dapper Plus Description.');
INSERT INTO #Product(Name, Description) VALUES ('C# Eval Expression', 'C# Eval Description');`

How we do it :

(SELECT Name ,ROW_NUMBER() over (partition by Name order by Name) RowNumber FROM #Product) a where a.RowNumber = 1;

Top comments (0)