I would like to filter a layer with the maximum value of field_a, grouped by field_b. I tried this expression: "field_a"= maximum ( "field_a","field_b") But it doesn't work (there are no more features in the layer). When I use this expression for Select by Expression, the features are selected. Is there a way to filter them (without selecting)?

[Edit] I want to filter a existing layer (right mouse click -> filter). I don't want to select features or to create a new layer.


The filter query (layer properties/source/provider feature filter) allows you to specify an SQL where clause. If you enter id=1 it will translate it to select * from mylayer where id=1.

From there, you can add a sub-query in this where clause to identify the IDs (or else) of interest:

field_a IN (SELECT MAX(field_a) FROM myLayer GROUP BY field_b)
  • Thank you very much, that is what I looked for!
    – Nane
    Oct 20 '19 at 13:38
  • @JGH sorry to ask but i don't see how to input SQL syntax in that part of the interface ... how do u do that ? where exactly are you ?
    – Snaileater
    Nov 4 '19 at 19:04
  • @snaileater layer properties, source, query builder
    – JGH
    Nov 4 '19 at 19:09
  • ok i was at the bottom of the attribute table ... but ... following your advice i don't succeed to enter full sql syntax (i mean using the FROM clause) ... there must be something i don't get ...
    – Snaileater
    Nov 4 '19 at 19:25
  • @snaileater I tested it with a Postgres layer and a Shapefile. It failed with an in-memory layer though, so this solution seems to be data provider-dependent
    – JGH
    Nov 4 '19 at 19:40

If this is for visualization purpose, I suggest Rule-based symbology.

"field_a"  =  maximum("field_a", group_by:= "field_b" )

enter image description here

[EDIT] To actually reduce the features by the expression, try Extract by expression tool (in Processing toolbox > Vector selection).

The expression is the same as the above.

  • No, it is not for visualization purpose. I want to reduces the number of features in the attribute table with a filter. And unfortunatly the expression does not work with the filter.
    – Nane
    Oct 20 '19 at 9:48
  • @Nane Got that, thanks for clarification.
    – Kazuhito
    Oct 20 '19 at 9:55
  • @ Kazuhita. Thank you for your help. Somehow I describe my problem not clear, sorry. I don't want to get a new layer, I just want to filter an existing layer (right mouse click on the layer, -> filter). But there this expression doesn't work...
    – Nane
    Oct 20 '19 at 10:27
  • @Nane Aah, sorry I misunderstood the question. I now understand this expression does not work,
    – Kazuhito
    Oct 20 '19 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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