7

I have data with a column containing a number value, and I want to divide the value of each feature by the total sum of all the values in the column (to get a percentage).

So I would like to know if there is some simple syntax in the QGIS field calculator to get the sum of a column, preferably without resorting to for loops and what not.


I have a shapefile where feature A has the value X in column B. How can I summerize the values for all features in column B in order to divide value X to get a percentage for each feature?

  • you can use count( "field1" , "field1" ) – FJ Monoarfa Mar 16 at 2:18
12

Update: QGIS 2.18 (07/07/2017):

In the Field Calculator, you can use the Aggregate function sum which allows you to sum the values of a column based on an expression. You can also, optionally, group and filter features for summation. So in your field calculator, you could simply use:

 (sum( "column_B" ) / "column_B") * 100


Original post: 23/04/2015

You can't sum columns using the Field Calculator (although it would be useful in certain situations). To be able to sum columns and obtain a value, you can use:

  • QGIS function - Basic Statistics (Vector > Analysis Tools > Basic Statistics)
  • QGIS Plugin - GroupStats (Plugins > Manage and Install Plugins)
  • QGIS Plugin - Statist (Plugins > Manage and Install Plugins)

So what I would do is create a new integer or real column (eg. "Sum") and add the value you obtain from any of the methods into the Expression window. Then create another column (eg. "Percentile") and edit the following expression to suit your column names:

("Sum" / "column_B") * 100
  • 2
    This works, although I was hoping for a more elegant solution. But I guess "you can't" is sometimes a useful answer, since you can stop looking for an elegant solution and go with the solution that actually solves the problem. – hexamon Apr 29 '15 at 12:46
  • 1
    @hexamon - I know exactly what you mean as I always tend to look for more elegant solutions first before giving up for an alternative method (if any!). – Joseph Apr 30 '15 at 9:11
  • Question: Let say I wanted to get a sum of a particular column would the syntax look like sum($columnName) ? – BPDESILVA Jun 27 at 5:27
  • 1
    @MRDR - You're almost correct! Just remove the dollar sign :) sum(columnName) – Joseph Jun 27 at 9:30

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.