# How can QGIS Field Calculator sum values from a spatially joined table?

Can the Field Calculater be used to sum values from a related table via a Spatial Join?

I have two vector polygon layers A and B, each with a numerical column VALUES. I want to update VALUES in layer A with the sum of VALUES from layer B where geometry from B is contained within geometry A.

So far I have the expression `geomcontains('Layer B','VALUES')` and what I want is to Sum this.

I have seen other ways to do this via plugins like SpatialJoin etc, but they all involve modifying either saving new layers or modifying the structure of Layer A, which I think is fairly cumbersome - is there a simple solution? (In MapInfo this is extremely simple, using Update Column.)

• (I have the refFunctions plugin installed, which includes the function 'geomcontains') Apr 16, 2015 at 13:24
• While there are ways to make them do so, Field Calculators typically don't work with multiple rows/records in a calculation - they're designed to operate on multiple fields within a single row. You're looking for basically a Summary Statistics tool as it's known in ArcGIS. The GroupStats plugin might meet your needs for QGIS. But you will have to generate a summary table and then join that back to A to update the fields. This question might also help: gis.stackexchange.com/questions/73681 Apr 16, 2015 at 21:39

You can use the toolbox for the spatial join (processing toolbox > search: join by location) and save the result to a temporary layer. The temporary layer is only saved in your QGIS project. Then you can join the temporary layer to the layer A.

You can find the processing toolbox in view > panels > toolbox or right click over the toolbars.

• In QGIS 3 this appears to be renamed "Join attributes by location (summary)" Oct 27, 2020 at 16:13

The refFunctions plugin offers `intersecting_geom_sum()` function to do that task.

Activate the Field Calculator on your `Layer A` and try,

``````intersecting_geom_sum('Layer B','VALUES')
``````

This function sums the geometries' attributes (`Values` in your case) of the target layer (your `Layer B`).