2

I have a table with thousands of objects but created an example Image for this question.

enter image description here

I need an expression that does the following.

  1. Select all objects that share the same values in field 1.
  2. Among them select the one with the highest value in field 2.
  3. From this object take the value of field 3.
  4. Add this value to field 4 from all selected objects from step 1.
  • How is this not a duplicate of your previous question? The title is nearly identical. – Vince Jul 2 at 12:58
  • So, field 4 = field 3?! – Erik Jul 2 at 13:04
  • I don't know how to choose the title short enough and still describing the question, but it's not the same question. It's an extention of my previous question I would say. Before it was "take the highest number of field 2 and write it to field 3". Now it's "choos the object with the highest number of field 2 and take it's value from field 3 and then add this value to field 4" – Dennis Jan Jul 2 at 13:07
  • So, add the value of field 3 to field 4, but only where 3 = 2. Also, the previous step doesn't seem to have worked, since field 3 has different values for equal field 1. – Erik Jul 2 at 13:21
  • I don't think we understand each other. It's like this "Pick field 3 from the highest field 2 of all objects that have the same field 1 and add that field 3 to all field 4 that have the same field 1". Sorry, I know it's confusing. Any idea how to write that? – Dennis Jan Jul 2 at 13:46
2
  1. Select all objects that share the same values in field 1.

Group by field 1. The aggregate functions all accept a group_by parameter.

  1. Among them select the one with the highest value in field 2.

Use the aggregate function maximum(expression[,group_by][,filter])

maximum("field 2", group_by:="field 1")
  1. From this object take the value of field 3.

Get field 3 when "field 1" = maximum("field 2", group_by:="field 1"). Use get_feature() to get the feature that meets that criterion. Use attribute() to get the attribute value of field 3 of that feature.

attribute(get_feature('thislayer','field 1',maximum("field 2", group_by:="field 1")),'field 3')
  1. Add this value to field 4 from all selected objects from step 1.

Use the Field Calculator to update field 4 using this expression:

concat('field 4',attribute(get_feature('thislayer','field 1',maximum("field 2", group_by:="field 1")),'field 3'))

Substitute your actual field names and layer names.

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.