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

5
  • 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 gis.stackexchange.com/questions/203463/…
    – Taras
    Commented Dec 9, 2021 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
    Commented Dec 9, 2021 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
    Commented Dec 9, 2021 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
    Commented Dec 9, 2021 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
    Commented Dec 9, 2021 at 13:10

2 Answers 2

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'
    )
2
  • Perfect, that's it. Thank you very much.
    – lesto
    Commented Dec 9, 2021 at 13:10
  • bitte, keine Ursache!
    – Taras
    Commented Dec 9, 2021 at 13:19
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'
)

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.