What im trying ot do is update the first instance of a record based on contact_id.
I have a table of email addresses and corresponding contact_id’s. Each contact may have more than one email address so the table can contain more than one instance of the same contact_id.
I want to update the corresponding is_primary field to ‘1’, when I hit the first instance of each distinct contact_id.
I’m not too concerned about what ends up being the primary email address as long as everyone in the database has one.
Table Example:
contact_id | email | is_primary
---------------------------------------------
1001 | john@world.com |0
1001 | desmond@world.com |0
1002 | person@life.com |0
1003 | preson@help.com |0
1003 | bad@sql.com |0
1003 | have@searched.com |0
So the result im looking for would be:
contact_id | email | is_primary
---------------------------------------------
1001 | john@world.com |1
1001 | desmond@world.com |0
1002 | person@life.com |1
1003 | preson@help.com |1
1003 | bad@sql.com |0
1003 | have@searched.com |0
Have tried creating a temp table and macthcing to that, but query updated all is_primary fields. I know im missing something basic here, but my sql skills are limited.
This technique joins the table against itself in a subquery, but it only matches one row (based on contact_id and email matching. The trick is that the subquery returns just one of the email addresses using MIN, theoretically the first one alphabetically (not reliable, but you said that didn’t matter).
I have tested this with good results.