Let us assume the following table: test
ids | l_ids | distance
-----------------+-----------
53 | {150,40} | {1.235, 2.265}
22 | {20,520} | {0.158, 0.568}
The positions of the two arrays (l_ids, distance) are dependent, meaning that 150 corresponds to 1.235, 40 corresponds to 2.265 and so on.
I want to get the min distance and the respective l_id. Therefore the result should be like this:
ids | l_ids | distance
----------------+-----------
53 | 150 | 1.235
22 | 20 | 0.158
By running this:
select l_ids, min(dist) as min_distance
from test, unnest(test.distance) dist
group by 1;
the result is:
l_ids | dist
------------+-----------
{150,40} | 1.235
{20,520} | 0.158
I want to get the position of the minimum value in the array of distance in order to get the respective id from the array of l_ids.
Any guideline? Thank you in advance
distancearray, values are unique? or may be{1.235, 1.235, 2.265}also happens?l_idsfor example:{150, 70, 40}and indistancethese values:{1.235, 1.235, 2.265}(1.235repeates)