I have a table containing distances from one ID (an address) to another ID (bus stops).

There table contains distances from all addresses to all different bus stops.

Now I want to select the feature where the distance between the certain address and the bus stop is the shortest.

In QGIS I've tried to do something like this:

enter image description here


  • "meter" is the distance column;
  • "fromID" is the address.

There are 16 different addresses and a little more than 6000 features in the table. What I need is a table with only 16 features.

You can, of course, use minimum function as well.

Can anybody help out here?

  • 1
    What happens when removing the white space between : and = in the group_by statement?
    – mgri
    Jan 18, 2017 at 10:13
  • Thanks for the quick reply! Actually it removed the syntax error, but unfortunately all the 6,000+ features are selected. Can you guide me a step further?
    – kwiwe
    Jan 18, 2017 at 10:22

1 Answer 1


You can use the following expression in the Select by Expression tool:

"meter" = maximum( "meter", "fromID" )

This selects all "meter" values which equals the maximum "meter" value found for each "fromID" group.

  • 1
    @HowToInQGIS - Thanks, I tested it on a much smaller attribute table so will be interesting to see if it works for the OP ;)
    – Joseph
    Jan 18, 2017 at 11:09
  • 1
    Just tested on 6000 features and it works! However, @kwiwe I suggest you to deactivate the layer from the Layers Panel when editing the fields because QGIS may crash when managing/rendering too many features.
    – mgri
    Jan 18, 2017 at 11:21
  • @HowToInQGIS - Nice one, thanks for testing! Also, good advice =)
    – Joseph
    Jan 18, 2017 at 11:23
  • 1
    Jup. It does the thing. Thank you so much for your time.
    – kwiwe
    Jan 18, 2017 at 13:55
  • 1
    @HowToInQGIS - Done! I was not aware of that. Quite new here :-).
    – kwiwe
    Jan 19, 2017 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.