ArcGIS 3.0.2: What tool can select the records with the highest values in a particular field?
For example, in the screenshot below, I couldn’t figure out which tool can select the FOUR records with the highest population size in the “population” field
Solved! Go to Solution.
I have edited the model to work as intended, please review comments in model.
I wonder if this would work in your model:
Maybe that's the kind of thing @MErikReedAugusta had in mind in my ROW_NUMBER idea.
Somewhat related: Adding sequential numbers for the selected features
Determine the threshold and use the select by attribute tool.
Can you elaborate on how "Determine the threshold" would be done in a model?
As long as your data is in a file geodatabase (so not a shape file) you can use a subquery to answer this.
An example is shown below, in this example ORN is the layer.
If you use the data engineering pane and calculate the stats for an integer field it will list the min and max values and let you select them with a right click.
What sql code can select the FOUR records of the highest population size from the “population” field?
My apologies I missed that you wanted the top four values. I'm not sure (but I could be wrong) that a file geodatabase can support the functions that would allow you to answer this question and you need to approach it in a two-step approach in modelbuilder, this how I would have done it.
I show this below, in my example I am processing a layer called ORN:
The Calculate Value tool is set up as-is, note it returns double:
The code block is:
def Fourth(layer):
# You need to set field to the appropriate field
field = "SourceID"
s = set()
with arcpy.da.SearchCursor(layer, field) as cursor:
for row in cursor:
if row[0] is not None:
s.add(row[0])
l = sorted(s, reverse=True)
ml = l[0:4] # first 4 values in sorted list
mv = min(ml) # Get min value
return mv
The select by attribute tool is:
This logic will select the top four values in the field sourceID, be aware that the selection may return a selection greater than 4 if you have duplicate values in your field. Also underlying assumption of the code is that you have a minimum of 4 unique values in your field.
Many thanks for the help
I couldn’t customize the code you have already provide to my local data. In addition to that, the content of the “calculate value” tool in my Pro appears to be different than yours as per the screenshot below.
I’m attaching my data along with the model I tried to build with no success
I submitted an idea: Use Select by Attributes to select greatest n records (FGDB)