16

I'm working with population data, and I have an issue when it comes to knowing the sum of a population inside the attribute table.

example

In my attribute table, I'd like to add all the numbers in the red box, i.e. all the numbers together from the column "UN_2015_E".

Is there a way to do so?

3 Answers 3

41

There are several approaches that I may suggest:


Approach 1 :

"Show statistical summary" tool from the Statistics panel (Ctrl+6).

Symbolized as a Sigma symbol(Sigma), placed in the main QGIS working window, and belongs to the Attributes Toolbar.

Show_statistical_summary

In the appearing window, one needs to choose the required layer (population data) and choose the field that has to be explored, seems like "UN_2015_E". The total number will be calculated just in the row called 'Sum'.

The same result can be achieved with View > Statistical summary.

Or as it was already mentioned by @Erik View > Panels > Statistics Panel.


Approach 2 :

Field Calculator with the following expression sum("UN_2015_E") in the Expression dialogue to get the result as a Preview.

field_calculator

In case rounded values are needed, the expression must be altered into round(sum("UN_2015_E"),0).


Approach 3 :

With a hint from @J.R, there is also a Virtual layer through Layer > Add Layer > Add/Edit Virtual Layer.

In the Query panel, a short statement has to be pasted, see the image below:

SELECT SUM("UN_2015_E")
FROM "Layer_Name"

Virtual_layer

To achieve rounded values, one can adjust the query like:

SELECT ROUND(SUM("UN_2015_E"))
FROM "Layer_Name"

Approach 4 :

PyQGIS via Plugins > Python Console (Ctrl+Alt+P) and the following command:

print(qgis.utils.iface.activeLayer().aggregate(QgsAggregateCalculator.Sum, 'UN_2015_E')[0])

See the aggregate() method of the QgsVectorLayer class for more details.

Another option is using native Python functions to calculate the sum of all NOT NULL values with a filter:

print(sum(filter(None,[f['UN_2015_E'] for f in qgis.utils.iface.activeLayer().getFeatures()])))

Additionally, one can also refer to a layer by its name:

print(sum(filter(None,[f['UN_2015_E'] for f in QgsProject.instance().mapLayersByName('Layer_Name')[0].getFeatures()])))

Approach 5 :

QGIS domestic statistical geoalgorithm "Basic statistics for fields" in the Processing Toolbox (Ctrl+Alt+T).

Choose the field "UN_2015_E" and get the result as Sum: ***.

basic_statistic


Approach 6 :

The SAGA's "Field Statistics" tool, which will calculate the sum as one of the parameters.

field_statistics


Approach 7 :

QGIS's Plugin called "Group Stats".

Group_Stats


References:

2
  • 1
    It is rare to see such a thorough and comprehensive and simple answer to a question. This is wonderful, thank you so much.
    – nittyjee
    Commented Nov 19, 2020 at 9:48
  • @nittyjee, always welcome!
    – Taras
    Commented Nov 19, 2020 at 9:50
9

Add the statistics panel (view - panels - statistics panel) to your window layout and then let it show you the statistics of your column.

0

I just found out you can create a new virtual field in your layer and use expression:

sum(field)

Not very clever, because each feature will have the same value in this column, but you can easily access it in other expressions or reports and it sticks with your layer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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