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;

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay