This question already has an answer here:

I have a layer (vector layer imported from a Shapefile) in QGIS 2.8.3 with a bunch of features with a single numerical attribute, 'level'. I would like to filter the features with a threshold that is a certain percentage of the maximum value. For instance, if the threshold is 20% I would like to perform an expression like:

"level" > .20 * max("level")

or more SQL like:

"level" > .20 * (select max("level") from table)

These obviously doesn't work in the query builder or expression based filter. Is there a way to do this, with a different syntax, or do I need to use a different tool?

marked as duplicate by Chris W, Kevin, BradHards, underdark Oct 31 '15 at 21:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @MarHoff - Just noticed your flag and yeah, I agree that the same method could be used. Will leave this up to the moderators :) – Joseph Oct 30 '15 at 11:23
  • Use Basic Statistics on your layer (Vector > Analysis Tools > Basic Statistics...), select your layer and relevant field and obtain your maximum value. Then in your filter, replace max() with the value so it would be something like "level" > (0.2 * 80) if your maximum value was 80. Also note that the expression builder might not like .20 so you would need to add a zero in front, 0.20. – Joseph Oct 30 '15 at 11:40
  • Thanks guys, that question is indeed quite related to mine and answers there seem to indicate it is impossible to do it automatically. You can already look up the maximum in the values list in the query builder, so the extra step of going through Basic Statistics is not really needed. If somebody wants to put this as an answer that I can accept go ahead, or also fine if those more experienced here feel it should be closed :) – sgvd Oct 30 '15 at 12:29
  • I don't know what is the best thing to do it's actually my first flag :P Had your layer been stored in PostGis I would have been pleased to provide a more elegant answer. But if it's not the case Joseph's answer is the best. IMHO something like a sum(layer,field) expression could also be a great feature request for upcoming version. Of course we can also imagine count(),avg(),etc... on the same intuition. – MarHoff Oct 30 '15 at 13:02

Browse other questions tagged or ask your own question.