AnsweredAssumed Answered

Max Date - How to return unique IDs with Max Date

Question asked by artesiagis on Aug 8, 2013
Latest reply on Apr 22, 2015 by Dan_Patterson
Branched from an earlier discussion
Python Beginner - Using version 10.1 and not an SDE install I have an atribute table with firehydrant inspection data. I am trying to create a layer that will only show the records grouped by fire hydrant number and then only display the most recent inspection date. When I execute the following code it runs fine and does in fact create the layer but it doesn't select the Max Date. If I have a record for Hydrant 100 with two dates I only want the most recent date to be created within the new layer.
Appreciate any assistance on this.

import arcpy
from arcpy import env
env.workspace=r"Z:\GIS_Data\gdb\GIS_Test.gdb"

fc="COA_FH_Inspections"
cursor = arcpy.UpdateCursor(fc)
idlist = []
datelist = []

for row in cursor:
    idlist.append(row.getValue('HydrID'))
    datelist.append(row.getValue('TestDate'))
    cursor.updateRow(row)

print idlist
print datelist

idunique = set(idlist)

#Make a layer from the feature class
arcpy.MakeFeatureLayer_management(fc, "lyr")

for i in idunique:
    if i == idlist:
        arcpy.SelectLayerByAttribute_management("lyr", "New_Selection", '"HydrID" = i')
        arcpy.SelectLayerByAttribute_management("lyr", "subset_Selection",'"TestDate" = (Select Max("TestDate") from "lyr"')


Again thank you - Terry

Outcomes