4

I found this question when looking for my answer Referring to the parent layer with aggregate functions in QGIS

I need to do something similar. In another words I need to join attributes by field value using the expressions in Field Calculator. I want values of a given layer to sum up based on the value of an attribute in an atlas. Here is my code:

aggregate(
    layer:=*child layer*,
    aggregate:='sum',
    expression:=*child layer attribute to be summed*,
    filter:=*Child layer attribute to check*=attribute(@parent,*parent attribute to check*)
    )

I am using the expression attribute(), but I don't know if this exists, it's just something I have seen in the linked topic, but it doesn't work.

There is probably a work-around using an aggregate within aggregate, but this seems too complicated.

To explain a little more. I have polygons (child layer) with values inside several areas (parent layer, also polygons). Previously I added the parent area codes to the child polygons polygons in the past based on location. Now I am getting incorrect sums based on aggregate filtered by location, I need to validate the calculations and I cannot figure out another way to check, so I need to sum it up by filed values (the ones I added also based on location). I probably should be looking on different ways to check my calculations, but this is the step I am at and need help.

1 Answer 1

7

It's a bit hard to tell exactly where the error is, perhaps syntax (when mixing up aggregate() and attribute() you need to be careful about when to use single vs double quotes to refer to layers), or where you've used the expression.

If I understand correctly, you have a parent layer attribute (e.g. ABC, XYZ), and you want to sum the child layer numeric values (e.g. 5, 10, 15) as a column in the parent layer, where parent layer attribute = child layer attribute (i.e. parent ABC = child ABC).

The example below shows how you would achieve that. Is this what you are after?


Existing parent_layer (with attribute parent_attribute) and child_layer (with attributes child_attribute and child_value_to_sum)

enter image description here

In the parent layer, open field calculator, create new field called summed_child_values and use the following expression - note when to use the single and double quotes

aggregate('child_layer',
          'sum',
          "child_value_to_sum",
          "child_attribute"=attribute(@parent,'parent_attribute'))

enter image description here

Result:

enter image description here


Replace above layer names and column names as appropriate

1
  • how can i transform the aggregate expression to have the same expression but with current_feature (@parent). When i create child in child layer have the attribute 'sum_child_value' in parent layer that updates over time ? Commented Jun 28, 2022 at 7:49

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.