Create layer of records with Max Date

914
2
08-08-2013 06:29 AM
TerryHiggins1
New Contributor III
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"')
Tags (2)
0 Kudos
2 Replies
StacyRendall1
Occasional Contributor III
You have posted this question twice. I have posted the answer on your other thread:
http://forums.arcgis.com/threads/90182-Max-Date-How-to-return-unique-IDs-with-Max-Date
0 Kudos
StacyRendall1
Occasional Contributor III
For some reason the other thread was mistakenly merged with a completely different thread. Here is the answer:
http://forums.arcgis.com/threads/90090-List-Broken-Data-Source-s-Path?p=319943&viewfull=1#post319943
0 Kudos