<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Selecting maximum Value based on other field using ArcPy in ArcMap Questions</title>
    <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072998#M2702</link>
    <description>&lt;PRE&gt;sql = "ORDER BY Id, Elevation DESC"&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I feel this line does the trick. The first Elevation Value for each ID is the Max_Elev value.&lt;/P&gt;&lt;P&gt;Joshua's script works perfectly for me.&lt;/P&gt;&lt;P&gt;What results/anomalies are you getting (screenshot)?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 27 Jun 2021 17:36:13 GMT</pubDate>
    <dc:creator>JayantaPoddar</dc:creator>
    <dc:date>2021-06-27T17:36:13Z</dc:date>
    <item>
      <title>Selecting maximum Value based on other field using ArcPy</title>
      <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072987#M2697</link>
      <description>&lt;P&gt;I want to populate a field (MaxElv) selecting the highest value from another field (Elevation) based on the Id field (Id). The data looks like this:&lt;/P&gt;&lt;PRE&gt;Id   Elevation   MaxElv
1     50      &lt;BR /&gt;1     72&lt;BR /&gt;1     83&lt;BR /&gt;1     66&lt;BR /&gt;2    105&lt;BR /&gt;3     27&lt;BR /&gt;3     24&lt;BR /&gt;2     98&lt;BR /&gt;1     46&lt;BR /&gt;3     22&lt;BR /&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;96&lt;BR /&gt;3     26&lt;BR /&gt;2     99&lt;BR /&gt;4     25&lt;BR /&gt;4     21&lt;BR /&gt;4     22&lt;BR /&gt;3     28&lt;BR /&gt;1     52&lt;BR /&gt;&lt;BR /&gt;I&amp;nbsp;have&amp;nbsp;develop&amp;nbsp;the&amp;nbsp;following&amp;nbsp;code:&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; import arcpy&lt;BR /&gt;... fc = 'Country'&lt;BR /&gt;... CountryList = [] &lt;BR /&gt;... with arcpy.da.SearchCursor(fc, ["Id"]) as cur1:&lt;BR /&gt;... for row1 in cur1:&lt;BR /&gt;... if row1[0] not in CountryList:&lt;BR /&gt;... CountryList.append(row1[0])&lt;BR /&gt;... &lt;BR /&gt;... for i in CountryList:&lt;BR /&gt;... ElevList = []&lt;BR /&gt;... where = """{} = {}""".format("Id", i)&lt;BR /&gt;... with arcpy.da.SearchCursor(fc, ["Elevation"], where) as cur2:&lt;BR /&gt;... for row2 in cur2:&lt;BR /&gt;... if row2[0] not in ElevList:&lt;BR /&gt;... ElevList.append(row2[0])&lt;BR /&gt;... ElevList.sort()&lt;BR /&gt;... max_Elev= ElevList[-1]&lt;BR /&gt;... where2 = """{} = '{}' AND {} = {}""".format("Id", i, "Elevation", max_Elev)&lt;BR /&gt;... with arcpy.da.UpdateCursor(fc, ["MaxElv"], where2) as cur3:&lt;BR /&gt;... for row3 in cur3:&lt;BR /&gt;... row3[0] = 1&lt;BR /&gt;... cur3.updateRow(row3)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;But&amp;nbsp;it&amp;nbsp;gives&amp;nbsp;the&amp;nbsp;following&amp;nbsp;error&lt;BR /&gt;Runtime error&lt;/PRE&gt;&lt;LI-CODE lang="c"&gt;&amp;gt;&amp;gt;&amp;gt; import arcpy
... fc = 'Country'
... CountryList = [] 
... with arcpy.da.SearchCursor(fc, ["Id"]) as cur1:
... for row1 in cur1:
... if row1[0] not in CountryList:
... CountryList.append(row1[0])
... 
... for i in CountryList:
... ElevList = []
... where = """{} = {}""".format("Id", i)
... with arcpy.da.SearchCursor(fc, ["Elevation"], where) as cur2:
... for row2 in cur2:
... if row2[0] not in ElevList:
... ElevList.append(row2[0])
... ElevList.sort()
... max_Elev= ElevList[-1]
... where2 = """{} = '{}' AND {} = {}""".format("Id", i, "Elevation", max_Elev)
... with arcpy.da.UpdateCursor(fc, ["MaxElv"], where2) as cur3:
... for row3 in cur3:
... row3[0] = 1
... cur3.updateRow(row3)


But it gives the following error
Runtime error 
Traceback (most recent call last):
File "&amp;lt;string&amp;gt;", line 20, in &amp;lt;module&amp;gt;
RuntimeError: An invalid SQL statement was used.&lt;/LI-CODE&gt;&lt;PRE&gt;&lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;string&amp;gt;", line 20, in &amp;lt;module&amp;gt;&lt;BR /&gt;RuntimeError: An invalid SQL statement was used.&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 16:45:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072987#M2697</guid>
      <dc:creator>badrinathkar</dc:creator>
      <dc:date>2021-06-27T16:45:04Z</dc:date>
    </item>
    <item>
      <title>Selecting maximum Value based on other field using ArcMap?</title>
      <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072986#M2703</link>
      <description>&lt;P&gt;I want to populate a field (MaxElv) selecting the highest value from another field (Elevation) based on the Id field (Id). The data looks like this:&lt;/P&gt;&lt;PRE&gt;Id   Elevation   MaxElv
1     50      &lt;BR /&gt;1     72&lt;BR /&gt;1     83&lt;BR /&gt;1     66&lt;BR /&gt;2    105&lt;BR /&gt;3     27&lt;BR /&gt;3     24&lt;BR /&gt;2     98&lt;BR /&gt;1     46&lt;BR /&gt;3     22&lt;BR /&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;96&lt;BR /&gt;3     26&lt;BR /&gt;2     99&lt;BR /&gt;4     25&lt;BR /&gt;4     21&lt;BR /&gt;4     22&lt;BR /&gt;3     28&lt;BR /&gt;1     52&lt;BR /&gt;&lt;BR /&gt;I&amp;nbsp;have&amp;nbsp;develop&amp;nbsp;the&amp;nbsp;following&amp;nbsp;code:&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; import arcpy&lt;BR /&gt;... fc = 'Country'&lt;BR /&gt;... CountryList = [] &lt;BR /&gt;... with arcpy.da.SearchCursor(fc, ["Id"]) as cur1:&lt;BR /&gt;... for row1 in cur1:&lt;BR /&gt;... if row1[0] not in CountryList:&lt;BR /&gt;... CountryList.append(row1[0])&lt;BR /&gt;... &lt;BR /&gt;... for i in CountryList:&lt;BR /&gt;... ElevList = []&lt;BR /&gt;... where = """{} = {}""".format("Id", i)&lt;BR /&gt;... with arcpy.da.SearchCursor(fc, ["Elevation"], where) as cur2:&lt;BR /&gt;... for row2 in cur2:&lt;BR /&gt;... if row2[0] not in ElevList:&lt;BR /&gt;... ElevList.append(row2[0])&lt;BR /&gt;... ElevList.sort()&lt;BR /&gt;... max_Elev= ElevList[-1]&lt;BR /&gt;... where2 = """{} = '{}' AND {} = {}""".format("Id", i, "Elevation", max_Elev)&lt;BR /&gt;... with arcpy.da.UpdateCursor(fc, ["MaxElv"], where2) as cur3:&lt;BR /&gt;... for row3 in cur3:&lt;BR /&gt;... row3[0] = 1&lt;BR /&gt;... cur3.updateRow(row3)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;But&amp;nbsp;it&amp;nbsp;gives&amp;nbsp;the&amp;nbsp;following&amp;nbsp;error&lt;BR /&gt;Runtime error &lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;string&amp;gt;", line 20, in &amp;lt;module&amp;gt;&lt;BR /&gt;RuntimeError: An invalid SQL statement was used.&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 15:22:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072986#M2703</guid>
      <dc:creator>badrinathkar</dc:creator>
      <dc:date>2021-06-27T15:22:02Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting maximum Value based on other field using ArcPy</title>
      <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072989#M2698</link>
      <description>&lt;P&gt;It would help if you could add the Python script using "Insert/Edit Code Sample" option.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JayantaPoddar_0-1624807999388.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/17099iCE8338F70A0176FB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JayantaPoddar_0-1624807999388.png" alt="JayantaPoddar_0-1624807999388.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 15:34:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072989#M2698</guid>
      <dc:creator>JayantaPoddar</dc:creator>
      <dc:date>2021-06-27T15:34:44Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting maximum Value based on other field using ArcPy</title>
      <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072990#M2699</link>
      <description>&lt;P&gt;Questions along these lines get asked fairly regularly on the Esri Community, probably every 4-6 months.&amp;nbsp; There is no singular way to solve the question, but some ways are much more efficient than others.&amp;nbsp; In your case, the approach is one of the less efficient ones, so I won't discuss your invalid SQL statement but suggest a different approach using SQL sorting.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;lyr = # name of layer or path to data set
flds = ["Id", "Elevation", "MaxElv"]
sql = "ORDER BY Id, Elevation DESC"

with arcpy.da.UpdateCursor(lyr, flds, sql_clause=(None, sql)) as cur:
    eid, elev, max_elev = next(cur)
    max_elev = elev
    cur.updateRow([eid, elev, max_elev])
    prev_eid = eid
    
    for eid, elev, _ in cur:
        if eid != prev_eid:
            max_elev = elev
        cur.updateRow([eid, elev, max_elev])
        prev_eid = eid
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 16:08:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072990#M2699</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-06-27T16:08:27Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting maximum Value based on other field using ArcMap?</title>
      <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072992#M2704</link>
      <description>&lt;P&gt;Question cross posted:&amp;nbsp; &lt;A href="https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using-arcpy/m-p/1072990" target="_blank"&gt;https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using-arcpy/m-p/1072990&lt;/A&gt;.&amp;nbsp; &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/189695"&gt;@badrinathkar&lt;/a&gt; , I suggest deleting this one since people have started responding to the other one.&amp;nbsp; In the future, Python-related questions that are not specific to ArcMap or Pro can be asked &lt;A href="https://community.esri.com/t5/python/ct-p/python" target="_blank"&gt;https://community.esri.com/t5/python/ct-p/python&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 16:42:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072992#M2704</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-06-27T16:42:22Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting maximum Value based on other field using ArcPy</title>
      <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072993#M2700</link>
      <description>&lt;P&gt;Sir, I have edited the post. Please see.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 16:46:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072993#M2700</guid>
      <dc:creator>badrinathkar</dc:creator>
      <dc:date>2021-06-27T16:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting maximum Value based on other field using ArcPy</title>
      <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072997#M2701</link>
      <description>&lt;P&gt;Sorry to say! But your script populates the MaxElv field only with the value of Elevation with which it strikes&amp;nbsp;first for the same Ids.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 17:28:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072997#M2701</guid>
      <dc:creator>badrinathkar</dc:creator>
      <dc:date>2021-06-27T17:28:14Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting maximum Value based on other field using ArcPy</title>
      <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072998#M2702</link>
      <description>&lt;PRE&gt;sql = "ORDER BY Id, Elevation DESC"&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I feel this line does the trick. The first Elevation Value for each ID is the Max_Elev value.&lt;/P&gt;&lt;P&gt;Joshua's script works perfectly for me.&lt;/P&gt;&lt;P&gt;What results/anomalies are you getting (screenshot)?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 17:36:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1072998#M2702</guid>
      <dc:creator>JayantaPoddar</dc:creator>
      <dc:date>2021-06-27T17:36:13Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting maximum Value based on other field using ArcPy</title>
      <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1073002#M2705</link>
      <description>&lt;P&gt;Sir, please see the attached figure.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Error1.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/17102i96A539DBDA770153/image-size/large?v=v2&amp;amp;px=999" role="button" title="Error1.png" alt="Error1.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 18:19:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1073002#M2705</guid>
      <dc:creator>badrinathkar</dc:creator>
      <dc:date>2021-06-27T18:19:58Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting maximum Value based on other field using ArcMap?</title>
      <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1073004#M2706</link>
      <description>&lt;P&gt;Sir, I don't find any option to delete the post. please guide me.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 18:32:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1073004#M2706</guid>
      <dc:creator>badrinathkar</dc:creator>
      <dc:date>2021-06-27T18:32:05Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting maximum Value based on other field using ArcPy</title>
      <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1073006#M2707</link>
      <description>&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/analysis/summary-statistics.htm" target="_blank"&gt;Summary Statistics (Analysis)—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Id&lt;/STRONG&gt; is your case field&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Statistics field&lt;/STRONG&gt; is Elevation with Maximum as the option (or whatever)&lt;/P&gt;&lt;P&gt;skip the maxelev stuff, just join the resultant table back if you need to and persist the join&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 18:37:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1073006#M2707</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-06-27T18:37:41Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting maximum Value based on other field using ArcMap?</title>
      <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1073009#M2708</link>
      <description>&lt;P&gt;I am not sure what is going on because the "Pro" cross-post is not redirecting to this one.&amp;nbsp; Maybe someone merged the two already.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 19:30:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1073009#M2708</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-06-27T19:30:42Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting maximum Value based on other field using ArcPy</title>
      <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1073010#M2709</link>
      <description>&lt;P&gt;I see you have an "FID" field, I am assuming you are working with shape files.&amp;nbsp; If you consult the documentation, &lt;A href="https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-data-access/updatecursor-class.htm" target="_blank" rel="noopener"&gt;UpdateCursor—ArcMap | Documentation (arcgis.com)&lt;/A&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;DISTINCT, ORDER BY, and ALL are only supported when working with databases. They are not supported by other data sources (such as dBASE or INFO tables).&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;In short, my approach won't work for you because you are working with shape files.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 19:36:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1073010#M2709</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-06-27T19:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting maximum Value based on other field using ArcMap?</title>
      <link>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1073021#M2710</link>
      <description>&lt;P&gt;I merged them&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 21:18:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using/m-p/1073021#M2710</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-06-27T21:18:48Z</dc:date>
    </item>
  </channel>
</rss>

