I've got two tables let's call them A and B. Their column names are identical. But, B is fully populated where as A is like a half-eaten apple. They have tons of fields but I'm only going to use one to keep it simple. They both have Company Code.
B has duplicates (because people don't like to delete data?). So, unfortunately I can't easily join the two together on company code because I get these repeats.
What I want to do is take only one distinct row from B for each company code. I only want the row with the highest index (it was updated last) for each company code.
Then, I want to join those rows with corresponding company codes in A. But, a little snag is that A has some company codes which are not in B that I need to have in my result set. B has most of the data, so I'm taking data from B based on the matching above. Of course, for those entries where the company code only exists in A the data will have to remain blank for certain fields.
So.. how would I do this? I was half-tempted to just do the logic outside of the SQL call. But, the function I call takes a resultset as a parameter and then generates a file from it. So, I would either have to rewrite the function, or create my own resultSet object putting in the data I want (could get pretty ugly).
Any ideas? I'm using MySQL, but I feel like if someone can give me a highish level solution I should be able to do translate that into MySQL.