Select Maximum Value - Model Builder

4908
4
Jump to solution
05-27-2013 12:47 PM
ShinoRah
New Contributor
Hi there,

I am new to model builder and am stuck on what is a seemingly simple task..

I'd like the model to select the feature with the maximum value so that I can export and use it as my starting point for least cost path iterations.

In my case, I just need the feature with the highest numerical value for Near_Dist to be selected.

I have looked into the sort tool  (sort by descending then select first row), using code blocks and search cursors for calculate field or the select tool,  building expressions in select by attributes using somethine like: "Near_Dist=(SELECT MAX(Near_Dist) FROM TableName)"

But I have been unsuccessful so I am resorting to this forum.  Please let me know if you have encountered this before / have any ideas on what I could do.

Thanks so much in advance!
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor
Hi there,

I am new to model builder and am stuck on what is a seemingly simple task..

I'd like the model to select the feature with the maximum value so that I can export and use it as my starting point for least cost path iterations.

In my case, I just need the feature with the highest numerical value for Near_Dist to be selected.

I have looked into the sort tool  (sort by descending then select first row), using code blocks and search cursors for calculate field or the select tool,  building expressions in select by attributes using somethine like: "Near_Dist=(SELECT MAX(Near_Dist) FROM TableName)"

But I have been unsuccessful so I am resorting to this forum.  Please let me know if you have encountered this before / have any ideas on what I could do.

Thanks so much in advance!


Use Summary and create an output table with the Max value, Join it to the original feature class/table (create a layer or tableview first), and select where OBJECTID of the join table is not Null using Select By Attribute, then remove the Join.  This is the way I always do Statistical selections, although usually I am doing it using a group field unique value as the join field rather than the summary output itself as the join field.  And even more frequently I am doing this technique to use the join to transfer a statistical output back to the original table using the Field Calculator.

If the join fails (because joins on double values is not always reliable and in which case you should not use =, but instead use >= to deal with imprecision) you could at least read the value in the output to a variable and then do a simple SQL expression directly on the original feature class:

Near_Dist >= %value% - .0000001

View solution in original post

0 Kudos
4 Replies
RichardFairhurst
MVP Honored Contributor
Hi there,

I am new to model builder and am stuck on what is a seemingly simple task..

I'd like the model to select the feature with the maximum value so that I can export and use it as my starting point for least cost path iterations.

In my case, I just need the feature with the highest numerical value for Near_Dist to be selected.

I have looked into the sort tool  (sort by descending then select first row), using code blocks and search cursors for calculate field or the select tool,  building expressions in select by attributes using somethine like: "Near_Dist=(SELECT MAX(Near_Dist) FROM TableName)"

But I have been unsuccessful so I am resorting to this forum.  Please let me know if you have encountered this before / have any ideas on what I could do.

Thanks so much in advance!


Use Summary and create an output table with the Max value, Join it to the original feature class/table (create a layer or tableview first), and select where OBJECTID of the join table is not Null using Select By Attribute, then remove the Join.  This is the way I always do Statistical selections, although usually I am doing it using a group field unique value as the join field rather than the summary output itself as the join field.  And even more frequently I am doing this technique to use the join to transfer a statistical output back to the original table using the Field Calculator.

If the join fails (because joins on double values is not always reliable and in which case you should not use =, but instead use >= to deal with imprecision) you could at least read the value in the output to a variable and then do a simple SQL expression directly on the original feature class:

Near_Dist >= %value% - .0000001
0 Kudos
ShinoRah
New Contributor
Thanks so much for those ideas.

I've used inline variables once before and then was when I was doing an iterator tutorial.

I'm not sure how I would read the value in the output to a variable.  I re-labelled the output of summary stats to %Value%

and then did a select by attributes with your SQL expression [Near_Dist >= %Value% - .0000001] but I got invalid SQL errors. 

Is there a step I am missing?

Thanks again!!
0 Kudos
RichardFairhurst
MVP Honored Contributor
Thanks so much for those ideas.

I've used inline variables once before and then was when I was doing an iterator tutorial.

I'm not sure how I would read the value in the output to a variable.  I re-labelled the output of summary stats to %Value%

and then did a select by attributes with your SQL expression [Near_Dist >= %Value% - .0000001] but I got invalid SQL errors. 

Is there a step I am missing?

Thanks again!!


The variable cannot use the direct output of the summary, which is a table, not a numeric value.  You can use a Feature Selection Iterator on the Summary output which would output a variable named %value% automatically and then the expression should work without the brackets.  Make the Select by Attribute operation have the Iterator output as a precondition. 

A python cursor could also read the record of the Summary output as an alternative if this was a python script.

Did you try doing a join of the summary output to the original table and using the ObjectID syntax?
0 Kudos
ShinoRah
New Contributor
I ended up using 'iterate row selection' on the summary stats output and using your SQL in the select by attributes expression.
It worked and you are awesome.

Thanks so much!
0 Kudos