Home > OS >  SQL Deduplicate rows based on one Column
SQL Deduplicate rows based on one Column

Time:10-29

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|

CodePudding user response:

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

CodePudding user response:

Resolved by code below

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

  • Related