I have this dataset that describes the cycleways of a city.

enter image description here

I want to measure the km of cycleway for each type of highway (residential, track, unclassified, ecc) and then calculate the percentage of km for each type (eg. if I have a total of 100 km of cycleways, 10 of which are paths, the percentage of paths is 10%)).

note: the geometry is a polygon

  • you wrote a note: geometry is a polygon, what are you referring to, please explain better.
    – pigreco
    Feb 6, 2022 at 16:37

2 Answers 2


To get the km of cycleway for each type of highway, use this expression:

sum("lunghezza", group_by:="highway")

To get the percentage of each highway type, use this expression:

sum("lunghezza", group_by:="highway") / sum ("lunghezza")*100

You can create a virtual layer with the query:

select  row_number() over() as id,
        sum(lunghezza) as lunghezza_sum,
        sum(lunghezza)/sum(sum(lunghezza)) over () * 100 as percentage 
        from yourpolygonlayername
        group by highway

enter image description here

  • what does the query "row_number() over() as id" do? I'm doing a project with QGIS for my thesis so I need to understand what I'm using. this works perfectly by the way, thank you
    – anea
    Feb 7, 2022 at 12:24
  • 2
    It just adds a row count, 1,2,3, .... It is to get a unique id column. I think QGIS wants one or it throws an error when trying to create the virtual layer.
    – BERA
    Feb 7, 2022 at 12:26

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.