# Summarizing column in QGIS field calculator?

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" ) Commented Mar 16, 2019 at 2:18

## 1 Answer

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
``````
• 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. Commented Apr 29, 2015 at 12:46
• @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!). Commented Apr 30, 2015 at 9:11
• Question: Let say I wanted to get a sum of a particular column would the syntax look like `sum(\$columnName)` ? Commented Jun 27, 2019 at 5:27
• @MRDR - You're almost correct! Just remove the dollar sign :) `sum(columnName)` Commented Jun 27, 2019 at 9:30