I need the result for every combination of (from_id, to_id) which has the minimun value and the loop matching a criteria.
So basically I need the loop that has the minim
Can't you do this a lot more simply?
SELECT
from_id,
to_id,
MIN(value)
FROM
myresults
WHERE
loop_id % 2 = 0
GROUP BY
from_id,
to_id
Or maybe I'm misunderstanding the question.
EDIT: To include loop_id
SELECT
m2.from_id,
m2.to_id,
m2.value,
m2.loop_id
FROM
myresults m2 INNER JOIN
(SELECT
m1.from_id,
m1.to_id,
MIN(m1.value)
FROM
myresults m1
WHERE
m1.loop_id % 2 = 0
GROUP BY
m1.from_id,
m1.to_id) minset
ON
m2.from_id = minset.from_id
AND m2.to_id = minset.to_id
AND m2.value = minset.value