2

I have a layer/table with several fields I need to sum together to create a value in another field. I have tried using aggregate() and sum() but I can't get the a correct syntax to be able to do the calculation.

E.g. "field6" will equal:

sum("field1" + "field2" + "field3" + "field4" + "field5")

All fields are integer.

1
  • Welcome to Geographic Information Systems! Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works
    – Ian Turton
    Jul 5 at 7:49
7

Try:

"field1" + "field2" + "field3" + "field4" + "field5"
3
  • I tried that version as well, but it didn't work either. I wish to use this as an expression in a form widget. When applying test data, values are not calculating the final field.
    – CnP
    Jul 5 at 2:11
  • 3
    Try to forget about widget and focus on learning the basics of software.
    – FelixIP
    Jul 5 at 5:13
  • To understand the difference: aggregate as well as sum creates the sum for all features for a certain attribute like: value of field1 of point1 + value of field1 of point2 + value of field1 of point3 etc. Thus it calculates the sum of one single attribute per column for all points (or lines or polygons). What you want, however, is the sum per row: value of field1 of point1 + value of field2 of point1 + value of field3 of point1 etc., thus summing different attributes for the same poinz.
    – Babel
    Jul 5 at 6:28
4

In Attributes Form, click the field (which you have created as field type integer or decimal) and enter Default value:

"f1"+"f2"+"f3"

(using your field names)

And when I create a new feature with values 4,5,6 it automatically calculates 15:

enter image description here

5
  • In your example, are your fields all integer? My fields 1-5 are integer and using constraints with Value Map (E.g. Value1 = 0, Label1 = "A", Value2 = 2, Label2 = "B"). When 'labels are selected, resulting value populates field. Is this correct?
    – CnP
    Jul 5 at 6:20
  • I have added 'required' on fields and wish to remove them now to test offered solutions but can't access the required table settings to remove 'required'. Sorry, but new to QGIS.
    – CnP
    Jul 5 at 6:22
  • Yes they are all integers. Labels shouldnt matter,
    – BERA
    Jul 5 at 7:12
  • I have set my fields to integer and used value maps to provide values to the table. When I add the fields together it is only listing each value as if concatenated (so in your example where it adds to 15, I am getting 456. If I change one field from value map to text edit or range then the sum works. Any ideas?
    – CnP
    Jul 6 at 5:41
  • 1
    Was able to get calculation for integer fields working by using the following: to_int( "f1" ) + to_int( "f2" ) + to_int( "f3" ) + to_int( "f4" ) + to_int( "f5" ) otherwise value would only concatenate.
    – CnP
    Sep 8 at 23:54
1

If you have a lot of attributes you want to sum up, there are alternatives to enumerating all of them (value_1 + value_2 + value_3...). Here are two possibilities to create the sum of value_1 + ... + value_6 (see screenshot below). The function array_sum() is available since QGIS 3.18.

  1. For attributes with similar names (e.g. differing just in a number), create an array of the fieldnames by concatenating the stable part of the name + the variable part with array_foreach():
   array_sum (
       array_foreach (
           generate_series (1,6,1),
              eval (
               'value_' || 
               @element
           )
       )
   )
  1. For attribute-namess that differ a lot (no clear pattern), simply create an array of all attribute names using map_akeys(attributes( )). In necessary, remove those field-names you want to skip (field id in this case):
   array_sum (
    array_foreach (
        array_remove_all( 
            map_akeys (
                attributes( )
            ), 
            'id'
        )
        ,
        eval(@element)
    )
)

Screenshot: with both expressions, you get the same result: the sum from value_1 to value_6, without the id: enter image description here

1
  • This looks like a handy function. Currently limited to using QGIS 3.16 for a 3rd party plug-in I am using.
    – CnP
    Jul 6 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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