Skip to content
Advertisement

Sql query to flter by column only if duplicate exists

I’m writing a query on data with a ‘provider_id’ column. The data is mostly unique except for some entries with all columns but ‘provider_id’ being unique. Most of the values for provider_id are 1 but when there are duplicates as described above I want to remove the row with provider_id = 1. There are only ever 2 duplicates at a time.

The query below lets me find all the duplicates but I’m not sure how to then select the correct row based off this.

How can I select out where pricing_provider != 1?

Ex:

I want:

Advertisement

Answer

If you care about duplicates, I would expect > 1, not > 0. Then, window functions are probably the best solution:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement