enter image description here

I wish to loop through this shapefile, and for each "COUNTY_NM", count the number of values corresponding to each "layer" value.

I want to output a CSV or equivalent which would be show the number of values per each county per year e.g. "Okanogan County, 2000:1, 2002:16, 2003:4" etc.

  • 2
    you can use vector analysis > statistics by categories
    – jbalk
    Commented Sep 1, 2021 at 21:13

2 Answers 2


You can use the "Statistics by categories" from the Vector analysis toolbox in QGIS.

Select your layer, leave 'Field to calculate statistics on' empty, select "COUNTY_NM" and "layer" as your 'Field(s) with catagories'

enter image description here

enter image description here


enter image description here


There is a possibility using a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer...

Let's assume there is a layer called 'points' with its attribute table, see image below.


With the following query, it is possible to show the number of values per each county per year.

SELECT "COUNTY_NM" || ' | ' || GROUP_CONCAT("layer" || ' : ' || n) AS res
    SELECT "COUNTY_NM", "layer", COUNT("layer") AS n
    FROM "points"
    GROUP BY "layer", "COUNTY_NM")
ORDER BY "COUNTY_NM", "layer", n

The output attribute table will look like


  • Do you know if it is possible to create a virtal layer in the modeler? I cant find a tool / algorithm for it
    – Bera
    Commented Sep 3, 2021 at 8:41
  • 1
    I think this can help you: Execute SQL in QGIS Graphical Modeler
    – Taras
    Commented Sep 3, 2021 at 8:45

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.