Here is my data:
ID FName LName data1 data2
1 John Doe xxx1
2 John Doe xxx2 yyy2
And here is my desired result:
ID FName LName data1 data2
1 John Doe xxx1 yyy2
In short, I have a table where are a lot of people, and that table is filled from multiple sources with different data and IDs. What I want is, for each duplicate I found and for each column in the table view if there is data present in that cell, then, if it exists, try to dump it to the oldest record for that person, if there is data, do nothing.
I don’t know if I made myself clear.
What should be the best approach to do this? Should I write a stored procedure or it can be done with a clever query I haven’t came up with yet?
You can solve this with a query using joins and window functions:
SQLFiddle
Try testing this approach with your real data against Igor’s custom aggregate to see which performs better.