i have a layer in QGIS "Network" with attribute table. I want to compare columns rows from each columns and create ine and fill it based on result from comparison. Untill now I did it with help in Excel: enter image description here

I'm looking for to this in QGIS with help of SQL query:

SELECT t1.*,

     CASE WHEN Rock_G_ID =
                `(SELECT Rock_G_ID`
                   ` FROM network t2`
               ` WHERE t2.rownum = t1.rownum - 1) THEN '1'`
     ELSE '-' END AS Class`
     FROM network t1`

This works for one column and can't get it for the both same time like the formula in Excel. How can I do this?

  • Try looking into window functions, such as lead() and lag(). Those functions respecively look at the previous row and the next row's value :)
    – GHRF
    Apr 16, 2018 at 14:22
  • @GHRF thank you for your suggestion , can you give me an example
    – AED EDA
    Apr 17, 2018 at 8:11

1 Answer 1


I Haven't had a chance to test this, but it should get you close...

    WHEN attribute(get_feature(  @layer_name,  "fid" ,  @row_number -1 ),  "Rock_G_ID" ) = "Rock_G_ID"
        THEN attribute(get_feature(  @layer_name,  "fid" ,  @row_number -1 ),  "NEWColumn" )
    WHEN attribute(get_feature(  @layer_name,  "fid" ,  @row_number -1 ),  "ID_G" ) = "ID_G"
        THEN attribute(get_feature(  @layer_name,  "fid" ,  @row_number -1 ),  "NEWColumn" ) + 1
  • thank you very much for your answer , i will test it tomorow
    – AED EDA
    Apr 26, 2018 at 12:39

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.