I am trying to select rows from a table where an index column has its minimum value within a partition, under the condition that this row has not already had a match in a previous partition. The selection should work in a kind of draw without replacing-manner.
Maybe it is better to clarify with an example. Consider the following table:
ID1 ID2 index
foo qux 1
foo quux 2
foo corge 3
bar qux 4
bar quux 5
bar corge 6
baz quux 7
baz corge 8
For every group in ID1, starting at the lowest index values, I want to pick the match from ID2 with the minimum index value for this partition (here: qux with an in index value of 1).
Now, for ID1=bar, the matching ID2 would be quux (with an index value of 5), since qux has already been picked (and matched to foo).
Similarly, baz would match with corgebecause quux was already matched to bar.
So the resulting query result should be
ID1 ID2 index
foo qux 1
bar quux 5
baz corqe 8
Note that not all values in ID1 have all possible values in ID2 and vice versa, also the subgroup sizes in ID1 and ID2 are not constant.
Any ideas how I could achieve this?
Edit: the values of ID1 and ID2 don't have any meaning, they are just completely random identifiers. I modified the example to make this clearer.
Edit2: the solution from @p3consulting looked very promising, but it doesn't work with the following data
ID1 ID2 index
a1 b1 1
a1 b2 2
a2 b3 3
a4 b4 4
where it only delivers index=1 and not index=3.