15

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?

Taras
  • 32,823
  • 4
  • 66
  • 137
Tim56
  • 1,032
  • 2
  • 10
  • 18

3 Answers3

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:

Taras
  • 32,823
  • 4
  • 66
  • 137
9

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

Erik
  • 16,269
  • 1
  • 24
  • 43
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.