3

I am looking for a command, that sums the value of the field "Baulast" if they have the same "ogc_fid" and if there is a Number in the field "WEA". If there is no number in the field "WEA" -> 'keine Baulast'. Maybe something like

if("WEA">NULL, sum("Baulast","ogc_fid"), 'keine Baulast')

So as seen on the Picture, I want to sum the fields that have same ogc_fid and have a number in "WEA".

enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208
lesto
  • 317
  • 1
  • 7
  • 1
    What is the type of your "Baulast" field? maybe use the sum(to_int("Baulast"), to_string("ogc_fid")). BTW comparison with NULL should be done as if("WEA" IS NOT NULL, sum(to_int("Baulast"), to_string("ogc_fid")), 'keine Baulast'), check https://gis.stackexchange.com/questions/203463/what-is-the-difference-between-null-and-is-not-null-in-qgis-filter-expression – Taras Dec 09 '21 at 12:20
  • To find features with the same ogc_fid number you may have to write a small python script. Do you need to put that into an new field, back into the Baulast field or make a new feature that combines all 3 of these features? – Cary H Dec 09 '21 at 12:27
  • I tried Taras answer and it worked. Just sort the data and select the features (as you have shown) and use the equation. I added a new field called "Result". – Cary H Dec 09 '21 at 12:56
  • Seems to havent work correctly.. the expression "WEA" IS NOT NULL seems to be not working correct. As shown on picture 2. Even if the Field ogc_fid 222 with WEA is NULL, its summarizing -> Baulast_Ber – lesto Dec 09 '21 at 12:56
  • Ah your last edit with the "if("WEA" IS NOT NULL" command at the beginning is doing the job. Awesome, thank you :) – lesto Dec 09 '21 at 13:10

2 Answers2

8

As an alternative you may try this expression:

if("WEA" IS NOT NULL,
    array_sum(
        array_agg(
            "Baulast",
            group_by:="ogc_fid",
            filter:="WEA" IS NOT NULL
            )
        ),
    'keine Baulast'
    )
Taras
  • 32,823
  • 4
  • 66
  • 137
4

Why not simply:

sum(
    expression:="Baulast",
    group_by:="ogc_fid",
    filter:="WEA" is not NULL
)

See the docs: https://docs.qgis.org/testing/en/docs/user_manual/expressions/functions_list.html#sum

Combine it with if:

if(
    "WEA" is not NULL,
    to_string(sum( -- since the field is a string, dont forget to convert the sum result to a string
        expression:="Baulast",
        group_by:="ogc_fid",
        filter:="WEA" is not NULL,
    )),
    'Keine Baulast'
)
MrXsquared
  • 34,292
  • 21
  • 67
  • 117