<?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 Select MAX of a field by group in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/select-max-of-a-field-by-group/m-p/1273236#M67291</link>
    <description>&lt;P&gt;HI all,&lt;/P&gt;&lt;P&gt;I'm trying to figure out how to get the maximum value of a field within a group; e.g. I want the record corresponding to the most recent site visit.&lt;/P&gt;&lt;TABLE border="1" width="97.8711798228615%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="16.666666666666668%" height="25px"&gt;ObjectID&lt;/TD&gt;&lt;TD width="8.333333333333334%" height="25px"&gt;Site_ID&lt;/TD&gt;&lt;TD width="4.166666666666667%" height="25px"&gt;SpecCode&lt;/TD&gt;&lt;TD width="16.05924978687127%" height="25px"&gt;SStatus&lt;/TD&gt;&lt;TD width="21.44075021312873%" height="25px"&gt;Mon_Date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="16.666666666666668%" height="40px"&gt;1&lt;/TD&gt;&lt;TD width="8.333333333333334%" height="40px"&gt;A&lt;/TD&gt;&lt;TD width="4.166666666666667%" height="40px"&gt;&lt;P&gt;Frog&lt;/P&gt;&lt;/TD&gt;&lt;TD width="16.05924978687127%" height="40px"&gt;&lt;P&gt;OCCU&lt;/P&gt;&lt;/TD&gt;&lt;TD width="21.44075021312873%" height="40px"&gt;&lt;P&gt;1/1/1999&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="16.666666666666668%" height="25px"&gt;2&lt;/TD&gt;&lt;TD width="8.333333333333334%" height="25px"&gt;A&lt;/TD&gt;&lt;TD width="4.166666666666667%" height="25px"&gt;Frog&lt;/TD&gt;&lt;TD width="16.05924978687127%" height="25px"&gt;UNOC&lt;/TD&gt;&lt;TD width="21.44075021312873%" height="25px"&gt;1/4/1999&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="16.666666666666668%" height="25px"&gt;3&lt;/TD&gt;&lt;TD width="8.333333333333334%" height="25px"&gt;A&lt;/TD&gt;&lt;TD width="4.166666666666667%" height="25px"&gt;Frog&lt;/TD&gt;&lt;TD width="16.05924978687127%" height="25px"&gt;OCCU&lt;/TD&gt;&lt;TD width="21.44075021312873%" height="25px"&gt;5/2/2008&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="16.666666666666668%" height="25px"&gt;4&lt;/TD&gt;&lt;TD width="8.333333333333334%" height="25px"&gt;B&lt;/TD&gt;&lt;TD width="4.166666666666667%" height="25px"&gt;Toad&lt;/TD&gt;&lt;TD width="16.05924978687127%" height="25px"&gt;UNOC&lt;/TD&gt;&lt;TD width="21.44075021312873%" height="25px"&gt;3/6/2007&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="16.666666666666668%" height="25px"&gt;5&lt;/TD&gt;&lt;TD width="8.333333333333334%" height="25px"&gt;B&lt;/TD&gt;&lt;TD width="4.166666666666667%" height="25px"&gt;Toad&lt;/TD&gt;&lt;TD width="16.05924978687127%" height="25px"&gt;OCCU&lt;/TD&gt;&lt;TD width="21.44075021312873%" height="25px"&gt;9/5/2014&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;EM&gt;In this case, I want to return the max date for each site, returning records #3 and #5.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;I'd like to be able to do it in either select by attribute or a search cursor.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm having trouble structuring my SQL clause(s) to make this happen.&lt;/P&gt;&lt;P&gt;&lt;A href="https://support.esri.com/en/technical-article/000008936" target="_blank"&gt;How To: Select minimum and maximum values in the Select By Attributes window (esri.com)&lt;/A&gt;&lt;/P&gt;&lt;P&gt;This is my query I've tried for select by attribute:&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;(SpecCode = 'Toad' Or SpecCode = 'Frog') And 
(SStatus = 'OCCU') And 
(Mon_Date = (SELECT MAX(Mon_Date) from [Table Name] GROUP BY Site_ID))&lt;/LI-CODE&gt;&lt;P&gt;I'd appreciate any pointers on this; I feel like it should be really easy and so far it hasn't been.&lt;/P&gt;</description>
    <pubDate>Wed, 29 Mar 2023 19:38:32 GMT</pubDate>
    <dc:creator>AlfredBaldenweck</dc:creator>
    <dc:date>2023-03-29T19:38:32Z</dc:date>
    <item>
      <title>Select MAX of a field by group</title>
      <link>https://community.esri.com/t5/python-questions/select-max-of-a-field-by-group/m-p/1273236#M67291</link>
      <description>&lt;P&gt;HI all,&lt;/P&gt;&lt;P&gt;I'm trying to figure out how to get the maximum value of a field within a group; e.g. I want the record corresponding to the most recent site visit.&lt;/P&gt;&lt;TABLE border="1" width="97.8711798228615%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="16.666666666666668%" height="25px"&gt;ObjectID&lt;/TD&gt;&lt;TD width="8.333333333333334%" height="25px"&gt;Site_ID&lt;/TD&gt;&lt;TD width="4.166666666666667%" height="25px"&gt;SpecCode&lt;/TD&gt;&lt;TD width="16.05924978687127%" height="25px"&gt;SStatus&lt;/TD&gt;&lt;TD width="21.44075021312873%" height="25px"&gt;Mon_Date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="16.666666666666668%" height="40px"&gt;1&lt;/TD&gt;&lt;TD width="8.333333333333334%" height="40px"&gt;A&lt;/TD&gt;&lt;TD width="4.166666666666667%" height="40px"&gt;&lt;P&gt;Frog&lt;/P&gt;&lt;/TD&gt;&lt;TD width="16.05924978687127%" height="40px"&gt;&lt;P&gt;OCCU&lt;/P&gt;&lt;/TD&gt;&lt;TD width="21.44075021312873%" height="40px"&gt;&lt;P&gt;1/1/1999&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="16.666666666666668%" height="25px"&gt;2&lt;/TD&gt;&lt;TD width="8.333333333333334%" height="25px"&gt;A&lt;/TD&gt;&lt;TD width="4.166666666666667%" height="25px"&gt;Frog&lt;/TD&gt;&lt;TD width="16.05924978687127%" height="25px"&gt;UNOC&lt;/TD&gt;&lt;TD width="21.44075021312873%" height="25px"&gt;1/4/1999&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="16.666666666666668%" height="25px"&gt;3&lt;/TD&gt;&lt;TD width="8.333333333333334%" height="25px"&gt;A&lt;/TD&gt;&lt;TD width="4.166666666666667%" height="25px"&gt;Frog&lt;/TD&gt;&lt;TD width="16.05924978687127%" height="25px"&gt;OCCU&lt;/TD&gt;&lt;TD width="21.44075021312873%" height="25px"&gt;5/2/2008&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="16.666666666666668%" height="25px"&gt;4&lt;/TD&gt;&lt;TD width="8.333333333333334%" height="25px"&gt;B&lt;/TD&gt;&lt;TD width="4.166666666666667%" height="25px"&gt;Toad&lt;/TD&gt;&lt;TD width="16.05924978687127%" height="25px"&gt;UNOC&lt;/TD&gt;&lt;TD width="21.44075021312873%" height="25px"&gt;3/6/2007&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="16.666666666666668%" height="25px"&gt;5&lt;/TD&gt;&lt;TD width="8.333333333333334%" height="25px"&gt;B&lt;/TD&gt;&lt;TD width="4.166666666666667%" height="25px"&gt;Toad&lt;/TD&gt;&lt;TD width="16.05924978687127%" height="25px"&gt;OCCU&lt;/TD&gt;&lt;TD width="21.44075021312873%" height="25px"&gt;9/5/2014&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;EM&gt;In this case, I want to return the max date for each site, returning records #3 and #5.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;I'd like to be able to do it in either select by attribute or a search cursor.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm having trouble structuring my SQL clause(s) to make this happen.&lt;/P&gt;&lt;P&gt;&lt;A href="https://support.esri.com/en/technical-article/000008936" target="_blank"&gt;How To: Select minimum and maximum values in the Select By Attributes window (esri.com)&lt;/A&gt;&lt;/P&gt;&lt;P&gt;This is my query I've tried for select by attribute:&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;(SpecCode = 'Toad' Or SpecCode = 'Frog') And 
(SStatus = 'OCCU') And 
(Mon_Date = (SELECT MAX(Mon_Date) from [Table Name] GROUP BY Site_ID))&lt;/LI-CODE&gt;&lt;P&gt;I'd appreciate any pointers on this; I feel like it should be really easy and so far it hasn't been.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Mar 2023 19:38:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-max-of-a-field-by-group/m-p/1273236#M67291</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-03-29T19:38:32Z</dc:date>
    </item>
    <item>
      <title>Re: Select MAX of a field by group</title>
      <link>https://community.esri.com/t5/python-questions/select-max-of-a-field-by-group/m-p/1273482#M67301</link>
      <description>&lt;P&gt;Postfix groupings can get ugly and are not supported by some types of data storage formats.&amp;nbsp; I'd do it in a cursor if you could for portability and just do the max by comparison:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;where = "(SpecCode = 'Toad' Or SpecCode = 'Frog') AND SStatus = 'OCCU'"

siteDict = {}
with arcpy.da.SearchCursor(tble, ['Site_ID', 'Mon_Date', 'SStatus', 'SpecCode'], where_clause=where) as sCur:
    for row in sCur:
        if not siteDict.get(row[0]):
            siteDict[row[0]] = row[1]
        else:
            if siteDict[row[0]] &amp;lt; row[1]:
                siteDict[row[0]] = row[1]

for k, v in siteDict.items():
    print(f'Site ID: {k} last visited: {v}')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 12:51:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-max-of-a-field-by-group/m-p/1273482#M67301</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-03-30T12:51:01Z</dc:date>
    </item>
    <item>
      <title>Re: Select MAX of a field by group</title>
      <link>https://community.esri.com/t5/python-questions/select-max-of-a-field-by-group/m-p/1273498#M67302</link>
      <description>&lt;P&gt;Trying a simple test I can only get a single selection even with a GROUP BY - which I guess is a scalar subquery result.&amp;nbsp; Trying to replicate your query with variations of ORs and ANDs just caused strange unexpected selections.&amp;nbsp; I don't think subqueries are the way to go with an FGDB.&amp;nbsp; Recommend Jeff's cursor.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 13:15:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-max-of-a-field-by-group/m-p/1273498#M67302</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2023-03-30T13:15:08Z</dc:date>
    </item>
    <item>
      <title>Re: Select MAX of a field by group</title>
      <link>https://community.esri.com/t5/python-questions/select-max-of-a-field-by-group/m-p/1274326#M67322</link>
      <description>&lt;P&gt;Doing more testing, I was able to mostly get it to work, with one issue remaining. The key was to use IN.&lt;/P&gt;&lt;P&gt;As it turns out, this data is not nearly as clean as it should be, and there are several cases of visit records with duplicate site# and Dates. I took care of that easily enough:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;OBJECTID In 
(select max(OBJECTID) 
from [table] 
WHERE Mon_Date In 
    (SELECT max(Mon_Date) 
     from [table] 
     Where Mon_Date is not null 
     Group BY Site_Id) 
GROUP BY Site_ID )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My issue now is that some of the dates are not correctly being evaluated by MAX(). Which means that this happens:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AlfredBaldenweck_1-1680293539585.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/66985iEB548CDBE3AADE39/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AlfredBaldenweck_1-1680293539585.png" alt="AlfredBaldenweck_1-1680293539585.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I'm at a loss here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2023 23:17:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-max-of-a-field-by-group/m-p/1274326#M67322</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-03-31T23:17:21Z</dc:date>
    </item>
  </channel>
</rss>

