2

I created a virtual field in my layer where I want a specific value to be calculated. In my attribute table I have several columns. The expression I am trying to create is the following. I want to iterate through all features and add all values of the column age who have a specific value, let's say the name of the features should be marc. In my first attempt I created this:

CASE WHEN "name" = "marc" THEN sum( "age" ) END

But it kinda doesn't work and I don't know how to iterate though all features in the layer and only choose those features with the name marc. Any advice?

asked Dec 7, 2017 at 10:52
3
  • Change the double quote "mark" to singe quote 'mark'. Beside is it mark in K or marc in c as in your question. Commented Dec 7, 2017 at 10:57
  • 1
    @ahmadhanb - If you do that and there is at least one value of mark in the field, you will sum the entire field including those features which do not have mark :) Commented Dec 7, 2017 at 11:06
  • 1
    @Joseph I see, I did not notice that. Thanks for your clarification. Commented Dec 7, 2017 at 11:10

1 Answer 1

5

You could use something like the following:

sum( "age", "name", "name" = 'marc' )

Where:

  • "age" is the field used to calculate the sum;
  • "name" is the field used to group the different names together;
  • "name" = 'marc' is the expression used to filter all names in the name field which equal marc.
answered Dec 7, 2017 at 10:58
4
  • 1
    It's better to update your answer to use 'marc' in 'c' since the asker modified his question. Commented Dec 7, 2017 at 11:36
  • In the expression builder I can see the right output, but when I try to get the value from my python script I always get None. Any ideas? Or should I post another question about this? Commented Dec 7, 2017 at 11:51
  • @Blinxen - I think you should post another question and include the script you used otherwise it is difficult to tell where the problem is :) Commented Dec 7, 2017 at 11:53
  • 1
    I created a new question and posted it. Commented Dec 7, 2017 at 12:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.