4

This question already has an answer here:

I need to calculate a score based on an area field using these ranges:

if >= 30000 (square meters) then 4 (score)  
if < 30000 AND >= 10000 then 3  
if < 10000 AND >= 5000 then 1  
if < 5000 then 0

I've tried with the CASE function but it only works for lines with a single operator (i.e. >=30000 then...) Lines with two operators (where I wrote AND) are seemingly not supported. Here is one of the syntaxes I've tried.

CASE 
WHEN Surface >= 30000 THEN 4 
WHEN Surface < 30000 >= 10000 THEN 3
WHEN Surface < 10000 >= 5000 THEN 1
ELSE 0
END

The result of this exact command in the score column are only 4 or 0. Which makes me understand that the first line works but not the others.

I know SQL has its BETWEEN operator, which would probably be a good solution to my syntax issue, but I don't see it in the field calculator.

So I'm wondering if another function could help me here or if I just need the correct syntax.

marked as duplicate by Taras, Jochen Schwarze, Kadir Şahbaz, neogeomat, whyzar Jul 25 at 13:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

Have you tried something like :

CASE 
  WHEN "Surface" >= 30000 THEN 4
  WHEN "Surface" < 30000 AND "Surface" >= 10000 THEN 3
  WHEN "Surface" < 10000 AND "Surface" >= 5000 THEN 1
  ELSE 0
END
2

Additionally to the @J. Monticolo's answer, you may try using if-statement

if("Surface" >= 30000, 4, if("Surface" >= 10000 and "Surface" < 30000, 3, if("Surface" >= 5000 and "Surface" < 10000, 1, 0)))

Not the answer you're looking for? Browse other questions tagged or ask your own question.