28

In QGIS 2.8 I have a large data set and I would like to search one field for any values over a certain amount (in this case 20) and if the value is larger than 20, I would like to return a value of 1 in another field, otherwise return a value of 0.

How can I implement it?

Taras
  • 32,823
  • 4
  • 66
  • 137
user35127
  • 399
  • 1
  • 3
  • 9
  • I'm trying to figure out this express in Qgis but it doesn't have results. Case WHEN "A" = '1' and "LENGHT" <= 1 then "C"= '4' ELSE ERROR END – giss Dec 01 '17 at 13:26

3 Answers3

50

The easy way

The most simple way to do this is to create a new field with the expression

"cat" > 20

This expression will evaluate to a boolean True/False which will be represented as an integer 1 or 0.

Virtual Fields

You can also create a virtual field, which will automatically return an updated value in case the values in cat change (e.g. you edit the layer). Remember that the values of virtual fields will not be saved in the dataset and are only visible inside this QGIS project.

More than boolean

If you have more than a simple "greater than", you need to use

CASE 
  WHEN "cat" > 100 THEN 2
  WHEN "cat" > 10 THEN 1
  ELSE 0
END
Matthias Kuhn
  • 27,780
  • 3
  • 88
  • 129
15

I just tested this : You can use the condition several times:

WHEN    cat =   1   THEN    205
WHEN    cat =   2   THEN    215
WHEN    cat =   3   THEN    225
WHEN    cat =   4   THEN    235
...

....

elmo
  • 809
  • 5
  • 19
10

Something like this perhaps:

case 
when "FIELD" > 20 then 1
else 0 
end

Field Calculator

Joseph
  • 75,746
  • 7
  • 171
  • 282