I want to group and count features with the same code attribute value.

I can classify the data and enable feature count in the layer list but I cannot get counts in the attribute table.


5 Answers 5


With a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer.... you can now use SQL on any layer in QGIS, for example the airport shapefile from the QGIS sample data:

enter image description here

To count the number of airports by USE type and compute the average elevation use:

enter image description here

The results are loaded as a new layer:

enter image description here

  • 1
    This was more useful to me than Group Stats, because I was trying to get data from a layer that joined a vector layer with fields from a attribute-only (CSV) layer. Group stats would only let me pull from a single layer rather than a join.
    – J. Taylor
    Commented Mar 5, 2018 at 18:55
  • I've just tried this exact method with a layer on my project, the SQL test comes back with no error, but all I'm getting in the 'sum' column is 0. Any clues as to where I might look for what I'm doing wrong?
    – user39790
    Commented Jun 15, 2018 at 8:51
  • 1
    @Isaacson, I was facing exactly the same issue, it turned out quotes were the ones to be blamed for. In my case double quoting of the fields was giving error when testing, single quotes were giving no error but wrong results, no quotes were giving the expected result. It might also depend on the version of QGIS you are using. I am on 3.4
    – dorakiara
    Commented May 23, 2019 at 10:13
  • 1
    P.S. Also I couldn't get it working to aggregate (SUM) on a joined filed.
    – dorakiara
    Commented May 23, 2019 at 13:58

This was the best result on Google I could find, but the screenshot tutorial by G M is out of date and it took me a while to work out how to use the plugin.

  1. Once installed, Load the Group Stats control panel from the "Vector" menu.
  2. Select the layer of interest.
  3. From the fields list, drag the field of interest into the "rows" box.
  4. Now drag the same field into the "value" box.
  5. In the fields list are also some functions, drag "count" into the "value" box (below the field you put in there).
  6. Click "calculate" and a table will appear on the left.
  7. You can sort by the count if you wish, click the column label.

How this helps other people.

  • How would you do this for a join between an attribute-only layer and vector layer? For instance, say I had a "Parcels" table that had property parcels and values, and then an "Owners" table that mapped owner names to parcel numbers. How would I say "Group all the owners by the sum of the property values of all the parcels they own"?
    – J. Taylor
    Commented Mar 5, 2018 at 8:37

QGIS 3.10 comes with a "Statistics by categories" tool in the QGIS's Processing Toolbox (Ctrl+Alt+T).

Statistics by Categories in QGIS

This is the result:

enter image description here


I suggest you to install Group Stats plugin. Here is a quick tutorial: group stat tutorial

  1. Search for the Group Stats icon
  2. Select the layer in which you have the Codes
  3. Select the attribute (in your case Code)
  4. Click on Calculate

The Count will appear. If you go to preferences you should be able to select only Count avoiding the other parameters to be displayed (this is not my case...). Remember before click Save button you have to select the fields you want to add to your .csv: hold on Shift for do that. Here is a more advanced tutorial written by Anita Graser.

  • You're welcome! Have a look to this question too. I hope to see more from you! Bye!
    – G M
    Commented Sep 9, 2013 at 9:22

you could also use Qspatialite in QGIS. The SQL window is just as Mapinfo one. Up to you after to make your SQL request with a groupby command on the wanted column.

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.