I'm working with map of plants in garden. This garden is divided into about 40 sections. Plants of one species have their number for example every Scots pine is 12 and every Common oak is 15 in field "Inventory". I want to number every plant of each species on each section in column "RecNum", for example:

example of result I want to get

This tabel is "hand-made", how to do it automatically in field calculator?


QGIS3 has a tool Add autoincremental field for this but it seems you need qgis-2.18 solution.

Someone may offer Python code. Let me suggest a Virtual Layer (or SQLite) workflow:


  1. Open the attribute table and add an unique id field (e.g. fid). Save and close the table.

Virtual Layer:

  1. Layer | Add Layer | Add/Edit Virtual Layer
  2. Import your layer (let me call it your_layer)
  3. In the Query window, copy and paste a syntax below.
  4. Click on Test and if there is no error, click on OK

Query syntax is:

    SELECT Count(*)+1
    FROM   your_layer AS T2
    WHERE T2.Inventory = T1.Inventory AND T2.fid < T1.fid
          AND T2.section = T1.section
   ) AS RecNum
 FROM your_layer AS T1


enter image description here

  • Thanks for both solutions, but I really need to do it in the same atribute table. In my table there will appear some new points, so I have to update it from time to time. Any ideas more?
    – ann
    Jan 7 '20 at 9:34
  • @ann When you posted the question a year ago, you added qgis-2.18 tag. Are you still bound to this version? If you have switched to QGIS3, then ` Add autoincremental field` tool would do what you need.
    – Kazuhito
    Jan 7 '20 at 10:11
  • Now I work in QGIS3 and I found that tool, thanks for that. However, it adds new field to my table. What should I do if I want to do the same but in a field that already exist? Also, what should I do if I add some new points and I want to continue the numbering?
    – ann
    Jan 8 '20 at 6:35
  • @ann I honestly do not have an idea. (In most cases geoprocessing tools avoid overwriting existing attribute fields, for good reason). You may replace old field by the new one, but continuing the numbering would be difficult. Would you post it as a new question?
    – Kazuhito
    Jan 8 '20 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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