artesiagis

Create layer of records with Max Date

Discussion created by artesiagis on Aug 8, 2013
Latest reply on Aug 12, 2013 by stacyrendall
New at Python - appreciate assistance pointing me in the right direction. Using version 10.1 - not SDE environment I need to group an attribute table of inspection data and only select the most recent date. The python script below does not provide the data I need. It executes and creates a layer but it has the same records as if I didn't run the script. Example if I have 5 records for ID 100 I only want to return the record with the most recent date. I have roughly 500 firehyrdrants and want to only select the distinct hydrant ID and then the most recent inspection date.
Thanks in advance

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"')

Attachments

Outcomes