Not so fast - another answer chooses stop_id arbitrary
That is why your question does not make sense. We can pull out stop_ids and have different line_id and direction. But we have no idea why we have stop_id.
create temp table test( line_id integer, direction char(1), stop_id integer); insert into test values (1, 'N', 1), (1, 'N', 2), (1, 'S', 1), (1, 'S', 2), (2, 'N', 1), (2, 'N', 2), (2, 'S', 1), (2, 'S', 2) ; select distinct on (line_id, direction) * from test; -- do this again but will reverse the order of stop_ids -- could it possible change our Robust Query?!!! drop table test; create temp table test(line_id integer,direction char(1),stop_id integer); insert into test values (1, 'N', 2), (1, 'N', 1), (1, 'S', 2), (1, 'S', 1), (2, 'N', 2), (2, 'N', 1), (2, 'S', 2), (2, 'S', 1) ; select distinct on (line_id, direction) * from test;
First select:
line_id | direction | stop_id ---------+-----------+--------- 1 | N | 1 1 | S | 1 2 | N | 1 2 | S | 1
Second choice:
line_id | direction | stop_id ---------+-----------+--------- 1 | N | 2 1 | S | 2 2 | N | 2 2 | S | 2
So, we left without stop_id grouping, but we have no guarantees why we got the one we did. All we know is that it is a valid stop_id. Any updates, inserts, or other material that is not guaranteed by RDMS may change around the physical order of the lines.
This is what I meant in the top comment. There is no known reason to pull one stop_id on top of another, but for some reason you need this stop_id (or something else) desperately.