Select rows based on a minimum in one column
Use DISTINCT ON:

SELECT DISTINCT ON (gid)
       id, gid, distance, time_interval
FROM   dis
WHERE  time &gt; 0  --  filtered just to take positive values (orig. Q)
ORDER  BY gid, time_interval; -- optionally more expressions to break ties


Details:


Select first row in each GROUP BY group?