My question is: I have two tables: table A has two columns (KeyA and Match) and table B has two columns (KeyB and Match). I want to compare with the “Match” column.
If table A has 3 rows with a particular “Match”, and table B has 2 rows, a JOIN will return me all the combinations (6 in this case). What I want it to do is match up as many as it can, and then NULL out the others.
So, it would match the first “KeyA” with the first “KeyB”, the second “KeyA” with the second “KeyB”, and then match up the third “KeyA” with NULL, since table B only has two rows for this “Match”. The order is actually irrelevant, just as long as 2 rows match up, and then one value from table A returns with a NULL for the table B value. This is not like an INNER or an OUTER JOIN.
I hope this makes sense, it was difficult to express clearly, and was hard to find keywords to search on.
EDIT:
An INNER/OUTER join would match all the table A values with all of the table B values it could. Once a B value is “used up” I do not want it to match it with any other A values.
Example:
Table A (KeyA, Match)
(1, “a”)
(2, “a”)
(3, “a”)
Table B (KeyB, Match)
(11, “a”)
(12, “a”)
Desired output (KeyA, Match, KeyB):
(1, “a”, 11)
(2, “a”, 12)
(3, “a”, NULL)
You can use
partition byto number the rows for each value of match. Then you can usefull outer jointo fill up rows per Match. For example:Working code at SE Data.