I have a table with columns: Date, Phone, Name, and Event
I need a query that will first recognize the duplicate phone entry, and then assign the name of whichever one has the earlier date to the event column.
I have a table with columns: Date, Phone, Name, and Event
I need a query that will first recognize the duplicate phone entry, and then assign the name of whichever one has the earlier date to the event column.
Use:
UPDATE x
SET x.event = y.name
FROM YOUR_TABLE x
JOIN YOUR_TABLE y ON y.phone = x.phone
JOIN (SELECT t.phone,
MIN(t.date) As min_date
FROM YOUR_TABLE t
GROUP BY t.phone
HAVING COUNT(*) > 1) z ON z.phone = y.phone
AND z.min_date = y.date
AND z.min_date < x.date
But it doesn't address if there are duplicates of the minimum value.