3

I have a column filled with multiple informations in each row :

> e.g. row 1 :( 'Sécurisation des intersections;Reprise du revêtement;Ajout de signalisation (M12, feu vélo, etc.)' )

> e.g. row 1 :(   'Plateau traversant;Sécurisation des intersections;Aménagement de traversées;Ajout de signalisation (M12, feu vélo, etc.)'  )

I would like to fill another column with the calculation of related value.

I will try to explain. The first field is filled with different type of road work, and it's possible that some sections needed multiple work. Therefore there is multiple type of road work in the same row separate with point comma.

Now I want to affect some price to those road work. I know the value for each type of road work and would like to calculate the sum of thoses different road work for each row.

I know how to write this :

IF "MESUACCOMP" LIKE '%Sécurisation des intersections%' THEN 1000
IF "MESUACCOMP" LIKE '%Reprise du revêtement%' THEN 5000
IF "MESUACCOMP" LIKE '%Ajout de signalisation (M12, feu vélo, etc.)%' THEN 12000

But how could I get a sum?

When those 3 case are encountered in the same line I want to have 1000+5000+12000 = 18000

This is a bit difficult to explain in English.

2
  • One way building on your current setup will be to update three new columns, one for each if statement, and then, in a new step, add together the three columns in a fourth column. I would use sql virtual layers instead (if your into sql) but the field calculator approach is also fine where, running in multiple steps.
    – Jakob
    Commented Apr 13, 2018 at 11:55
  • I have not only 3 but more or less 10 type of road work. I have also considered to create 10 new columns but I would like to know if there is an "easier" way to do it. With field calculator I don't know how to explain I wan't the sum of these conditional expressions?
    – Arthur
    Commented Apr 13, 2018 at 11:58

1 Answer 1

3

In the field calculator, you can achieve that with something like:

IF("MESUACCOMP" LIKE '%Sécurisation des intersections%', 1000, 0) +
IF("MESUACCOMP" LIKE '%Reprise du revêtement%', 5000, 0) +
IF("MESUACCOMP" LIKE '%Ajout de signalisation (M12, feu vélo, etc.)%', 12000, 0)

(updated after JCH's comment)

3
  • Yes I'm trying to work with the field calculator in QGIS but just gave an exemple which could be false. I'm trying your syntax right away
    – Arthur
    Commented Apr 13, 2018 at 12:04
  • no need for the multiplication, you can do IF("MESUACCOMP" LIKE '%Sécurisation des intersections%', 1000, 0)
    – JGH
    Commented Apr 13, 2018 at 12:23
  • @JGH, yes, of course you're right. I'll update the answer.
    – ArMoraer
    Commented Apr 13, 2018 at 12:28

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.