Geographic Information Systems Asked by Adrian Alonso-Frontela on December 24, 2020
How can I "extract by expression" the N first (10 highest values of one column, for example)
I need it for the graphical modeler.
Use this expression:
array_foreach ( -- iterate over all entries for each value of the series created in the next line
generate_series ( 0 , 9 , 1) , -- generates a series from 0 to 9 with steps of 1 to get the first 10 values
array_get ( -- get the n-th entry (0 for the first) of the sorted array from the next line
array_sort( -- sort the array created in the next line
array_agg ( "value"), -- aggregate all values from the field "values" to an array
false ) , -- override default sorting order (ascending=true) to get a descending order
@element -- get the n-th entry of the sorted array, whereas n is the current value of the series (0 to 9)
)
)
See screenshot: I have an attribute "value" with random values from 1 to 2000. Output of the expression in my case is: [2000,1998,1996,1978,1978,1976,1961,1958,1956,1955]
. The output is obviously an array, so depending on what you neet it for, you must convert it.
As MrXsquared suggested, you can use array_contains(array,"value")
(replace array with the expression from above) to generate a boolean value to your attribute table with true for the n first (10 highest) values. Without that, the output is an array cotaining the 10 highest values.
Answered by babel on December 24, 2020
If you wish to use "extract by expression" you can use this expression:
"yournumericalfield" >= array_get(array_sort(array_agg("yournumericalfield"),false),9)
Replace "yournumericalfield" with the fieldname you wish to use as reference and replace 9
at the end of this expression with the highest wanted value. In this example it will extract the features with the 10 highest values.
If you would want to extract the 10 lowest values, you could replace false
with true
.
In case your field is not numerical, but contains numbers, you can add to_real()
to the expression.
Answered by MrXsquared on December 24, 2020
You can try the QGIS processing algorithm Execute SQL
(documentation) with :
SELECT * FROM input1 ORDER BY YOUR_FIELD desc LIMIT 10
SELECT *
: select all data from your layerFROM input1
: according to the documentation, input1
refers to the first input datasource, so your layerORDER BY YOUR_FIELD
: sort your data result according to YOUR_FIELD
, a field of your layer. You can add the word DESC
at the end for reverse orderLIMIT 10
: take only the 10 first recordsThis processing will create a new layer with the extracted records.
Answered by J. Monticolo on December 24, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP