6

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.

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

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

Output:

enter image description here

7

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.

input

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
FROM (
    SELECT "COUNTY_NM", "layer", COUNT("layer") AS n
    FROM "points"
    GROUP BY "layer", "COUNTY_NM")
GROUP BY "COUNTY_NM"
ORDER BY "COUNTY_NM", "layer", n

The output attribute table will look like

output

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

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.