Jing Cao
Jing Cao

Reputation: 11

SQL Deduplicate rows based on one Column

I have an ad name column and URL column, I just want to keep the unique ad name, with any of the URL variants. They are shortened varied URL but actually redirect to same URL.

How do I use SQL to return a table with unique ad names and its corresponding URL. I just need one URL for each ad.

Ad name URL
ad name 1 URL variant 1
ad name 1 URL variant 2
ad name 1 URL variant 3
ad name 2 URL variant 1
ad name 2 URL variant 2
ad name 2 URL variant 3

Upvotes: 1

Views: 271

Answers (2)

Jing Cao
Jing Cao

Reputation: 11

Resolved by code below

SELECT ad_name, Max(URL) as URL FROM table Group BY ad_name

Upvotes: 0

The Impaler
The Impaler

Reputation: 48810

You don't mention the database, so I'll assume it's PostgreSQL.

You can do:

select distinct on (ad_name), ad_name, url from t order by ad_name

Upvotes: 1

Related Questions