# Summing values of different layers based on another

I have a layer (called here 'layer_top') made of multiple polygons (in orange in the picture). Each polygon of the 'layer_top' overlaps with polygons of other different layers (called here 1,2,3,...). Each of these layers, within the 'layer_top', has a column with a score value (like in the picture). I would like to get the sum of the scores of all polygons of the layers 1,2,3, ... contained in each polygon of the layer_top. I have searched for a solution in this forum and tried to join attributes by location (including summary), but the result is not what I want.

• are there any preferences regarding the geometric predicate between polygons e.g. they must be fully inside or partial overlap is also allowed ? gis.stackexchange.com/questions/217444/… Commented Dec 5, 2022 at 15:28
• Thank you for the comment. Fully inside Commented Dec 5, 2022 at 15:49
• See my answer. Depending on the number of layers you have in your project, their name as well as the name of the attributes, you could use `array_foreach()` function to avoid repeating the same part over and over again. If interested, please provide information about the number of layers, their names and name of attributes. Commented Dec 5, 2022 at 16:31
• thank you. Where do you use that function? in the field calculator of the 'layer_top'? the number of layers within each polygon of the 'layer_top' are different (range between zero and ten). The name of the attributes are different except for the name of the score column Commented Dec 5, 2022 at 18:39
• Ok, then probably indeed naming layers and attribute field manually is the best solution Commented Dec 5, 2022 at 18:51

Use QGIS expressions with `overlay_contains()`. Use this expression where score1-3 are the names of the attribute you want to get the sum from:
``````array_sum (overlay_contains ('layer1', score1)) +