16

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.

16

With the new 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

  • 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 Mar 5 '18 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? – Isaacson Jun 15 '18 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 May 23 at 10:13
  • 1
    P.S. Also I couldn't get it working to aggregate (SUM) on a joined filed. – dorakiara May 23 at 13:58
7

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 Mar 5 '18 at 8:37
6

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 Sep 9 '13 at 9:22
1

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, privacy policy and cookie policy

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