6

Regarding SQL expressions in QGIS:

Based on the value of the field "_majority", I want to subtract the value of the field with the corresponding name from the field "_count".

Using the image below as an example, as the value in "_majority" is 5, I want to select field "5" and subtract the corresponding value of 25177 from the "_count" field.

Enter image description here

Is this possible using an expression in QGIS's Field Calculator?

2 Answers 2

10

That is a little tricky but doable, since you cannot turn the content of a field into a fieldname. But you can use maps as a workaround:

map_get(attributes($currentfeature), "_majority")

This will first turn your current feature into a map of keys (fieldnames) and values (fieldvalues). Here you can use the value of "_majority" to get the value of the corresponding key by using map_get().

So your final expression will be "_count" - map_get(attributes($currentfeature), "_majority")

0
5

You can get the target field name using "majority", and then you can evaluate (eval()) the value of the record at this field. To make is a field name and not a simple string (or number here), one would concatenate (||) double quotes around it.

eval('"'|| "majority" ||'"')

or for the whole expression

"_count" - eval('"'|| "majority" ||'"')

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.