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
    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
    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
    Dec 15, 2022 at 15:50
  • To Hjall: no, doesn't work.
    – GeoEth
    Dec 15, 2022 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.