If duplicates exist, select the value based on another column

ChrisG

I have a large list of zip codes and territories that I've combined from two different data sources.

My columns look like: zipcode, territory, source

The values might look like:

76345, ShiPaTown, Source1
76345, ShiPaTown, Source2
12110, South Park, Source1
12110, Mars, Source2

My objective is to only have ONE row per unique zip code and if there's a record for a zip code in BOTH Source1 and Source2, to always take the territory from Source1.

So the previous list would get reduced to:

76345, ShiPaTown
12110, SouthPark
Gordon Linoff

This is a prioritization query. Here is one approach:

select zip, town
from t
where source = 'source1'
union all
select zip, town
from t
where source = 'source2' and
      not exists (select 1 from t as t2 where t2.zip = t.zip and t2.source = 'source1');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

deleting duplicates based on value of another column

From Dev

remove duplicates based on the value of another column

From Dev

SQL Statement to eliminate duplicates based on value in another column

From Dev

SQL: Select the latest datetime value when duplicates found on another column

From Dev

Select statement that duplicates rows based on N value of column

From Dev

Deleting duplicates based on another column

From Dev

Remove duplicates in one column based on another column

From Dev

SELECT Mysql - Replacing value in one column based on another column

From Dev

SQL Server - Select column to update based on another column value

From Dev

select mysql column based on occurrence and value in another column

From Dev

mysql SELECT. If value in WHERE does not exist, then SELECT based on another (value) WHERE

From Dev

How to delete one of the duplicates based on another column?

From Dev

Excel duplicates in one column based on another

From Dev

How to select a bit based on value in another tables column in SQL

From Dev

Select latest value of another column based on a comparison between unique keys

From Dev

Remove duplicates from dataframe, based on two columns A,B, keeping row with max value in another column C

From Dev

select distinct records where multiple rows exist for one ID based on values in another column

From Dev

Remove Duplicates by Unique Value in another Column

From Dev

mysql - select without duplicates for every value in column

From Dev

SQL select column value with biggest number of duplicates

From Dev

Select value that does not exist in another column of the same table, MySQL Database Not Exists Clause

From Dev

updating column value based on another column value

From Dev

select a column value that matches with a value of another column

From Dev

Query to select duplicates in column 2 based on column 1 in MySQL

From Dev

Query to select duplicates in column 2 based on column 1 in MySQL

From Dev

Keep row if cell value exist in another column

From Dev

extracting value based on another column

From Dev

Select lines based on value in a column

From Dev

Conditional select based on column value

Related Related

  1. 1

    deleting duplicates based on value of another column

  2. 2

    remove duplicates based on the value of another column

  3. 3

    SQL Statement to eliminate duplicates based on value in another column

  4. 4

    SQL: Select the latest datetime value when duplicates found on another column

  5. 5

    Select statement that duplicates rows based on N value of column

  6. 6

    Deleting duplicates based on another column

  7. 7

    Remove duplicates in one column based on another column

  8. 8

    SELECT Mysql - Replacing value in one column based on another column

  9. 9

    SQL Server - Select column to update based on another column value

  10. 10

    select mysql column based on occurrence and value in another column

  11. 11

    mysql SELECT. If value in WHERE does not exist, then SELECT based on another (value) WHERE

  12. 12

    How to delete one of the duplicates based on another column?

  13. 13

    Excel duplicates in one column based on another

  14. 14

    How to select a bit based on value in another tables column in SQL

  15. 15

    Select latest value of another column based on a comparison between unique keys

  16. 16

    Remove duplicates from dataframe, based on two columns A,B, keeping row with max value in another column C

  17. 17

    select distinct records where multiple rows exist for one ID based on values in another column

  18. 18

    Remove Duplicates by Unique Value in another Column

  19. 19

    mysql - select without duplicates for every value in column

  20. 20

    SQL select column value with biggest number of duplicates

  21. 21

    Select value that does not exist in another column of the same table, MySQL Database Not Exists Clause

  22. 22

    updating column value based on another column value

  23. 23

    select a column value that matches with a value of another column

  24. 24

    Query to select duplicates in column 2 based on column 1 in MySQL

  25. 25

    Query to select duplicates in column 2 based on column 1 in MySQL

  26. 26

    Keep row if cell value exist in another column

  27. 27

    extracting value based on another column

  28. 28

    Select lines based on value in a column

  29. 29

    Conditional select based on column value

HotTag

Archive