With QGIS >=2.16, it is possible to use aggregates in expressions, such as sum, average... etc.

But when I use it to create a new virtual field using the field calculator, the newly created field value is null for all of the layer's entities.

An example here : field calc example

Am I doing it wrong ? Or it is a bug ?

I tried to change the field type with no effect. Note that it works when you create a non-virtual field.

  • 1
    I confirm that it does not work on with qgis 2.18 my computer either. You should probably file a bug report. – Victor Nov 8 '16 at 11:06
  • I wanted to visually group features by some id by editing an attribute and have instant feedback on some aggregate value (e.g. sum of an attribute of features sharing the same group id). I came up with an alternative : having two layers in my project, a layer which target the postgis table I want to edit, and another one which is a view with the aggregates. I think there is a smarter way to do that but I know little about qgis. – tumasgiu Nov 15 '16 at 9:42
  • I didn't know that qgis could edit postgresql views so only the view layer remains in my project now. – tumasgiu Nov 16 '16 at 8:43
  • Did anyone submit a bug report/figure out a solution? – sid-kap Nov 15 '17 at 2:53
  • The solution I've came up with is creating a view in db with triggers transmitting updates to the table, and editing that view in qgis. This is working only for RDBMS layers I guess. I wanted to fill a bug report, but I finally forgot to do that. – tumasgiu Nov 16 '17 at 8:20

Fisrt, your field needs to be quoted.

Now look this example, the first one is to add the sum of all the field in each row; the second one, is to sum by row in a specific field.

You have Integer or Real (decimal number) to set your field, depending on what you need to get.


| improve this answer | |
  • Hi, as I can say quoting is optional. And you're not creating a virtual field in your examples. I was aware that it would work the way you did. I'm looking to make it work with the "Create virtual field" box ticked. Thanks :) – tumasgiu Nov 7 '16 at 14:27

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.