4

There is a shapefile about facilities with columns "country" and "annual production".

I am looking for an expression for a calc field to summarize total production for all countries.

For example: if a country has 5 facilities, I need to sum up each facility for the total production of this country.

Is there some expression to do that?

enter image description here

2 Answers 2

10

To calculate the sum per country, use this expression:

sum("production", group_by:="country")

Replace "production" and "country" with the field names you have.

2
  • 1
    Thanks, it works, but "country" does not need to replace, it must stay like in Your example.
    – GeoEth
    Commented Dec 15, 2022 at 17:30
  • 2
    Yeah, as long as you use the same names, they can stay like this in the expression, of course
    – Babel
    Commented Dec 15, 2022 at 17:49
0

Try the Sum function:

 sum("annual production","country")

I feel this should also work with the aggregate function:

 aggregate('[[your layer name]]',sum,"annual production","country"=@parent)
2
  • 3
    I think there are some errors in the second expression, take a look to the aggregate documentation. The correct expression is. aggregate('[[your layer name]]', 'sum', "annual production", "country"=attribute(@parent, 'country'))
    – Mayo
    Commented Dec 15, 2022 at 15:50
  • To Hjall: no, doesn't work.
    – GeoEth
    Commented Dec 15, 2022 at 17:26

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.