I have features and each of them has an attribute about what it is. In my case, I have points representing trees and each tree has its species name as an attribute.
I would like to group them by species name and using numbers from 1 to n as seen on picture and if possible, not by hand because sometimes I have hundreds of species. I was thinking of some kind of expression which includes $rownum and tried a few things but no results. SQL queries also came to mind, but I have limited experience with them and I've never given them a try in QGIS.
I really don't want to export data to a spreadsheet, I would like to solve this in QGIS if possible.
Best Answer
There is an easy way that needs two steps from the db-manager and two tables that can be deleted in the end(there is a way with virtual tables, but i donĀ“t get them to work properly from the db-manager).
go to db-manager and then to virtual layers and select your table with the trees; then start the sql-window
Enter into the query field: SELECT distinct(species) FROM "your table name"
run it, and save it as a new table (for example temp1) with the button down below
SELECT species,
rowid AS number from temp1
run it and save it as temp2
now you can join your table temp2 to your species-table by using the column species (via the joins function from your layer-preferences); Qgis does create a 1:n join and therefore the numbers are copied for each species
use the field calculator to add the numbers to your species table, then you can delete the temp1 and temp2