OK. I did some testing, and here below is the final code which should work. Remember that you will still need to do something with the selected values, such as exporting to a new feature class...import arcpy
from arcpy import env
env.workspace=r"Z:\GIS_Data\gdb\GIS_Test.gdb"
fc="COA_FH_Inspections"
cursor = arcpy.SearchCursor(fc)
idlist = []
for row in cursor:
idlist.append(row.getValue('HydrID'))
del row, cursor
print idlist
idunique = set(idlist)
print idunique
#Make a layer from the feature class
arcpy.MakeFeatureLayer_management(fc, 'lyr')
for ID in idunique:
arcpy.SelectLayerByAttribute_management('lyr', 'ADD_TO_SELECTION', '"HydrID" = %s AND "TestDate" = (SELECT MAX("TestDate") FROM %s WHERE "HydrID" = %s)' % (ID, fc, ID))
# EXPORT RESULTS TO ANOTHER FEATURE CLASS
#, i.e. CopyFeatures_management
This will only work if the HydrID values are integers (Short or Long). If they are text you will need to add single quotes and escape them, like so: arcpy.SelectLayerByAttribute_management('lyr', "ADD_TO_SELECTION", '"HydrID" = \'%s\' AND "TestDate" = (SELECT MAX("TestDate") FROM %s WHERE "HydrID" = \'%s\')' % (IDval, fc, IDval))
Note that the code in my earlier reply had the closing parenthesis in the incorrect place, it had:arcpy.SelectLayerByAttribute_management('lyr', 'ADD_TO_SELECTION','"HydrID" = %s And "TestDate" = (SELECT MAX("TestDate") FROM %s) WHERE "HydrID" = %s' % (ID, fc, ID))
rather than:arcpy.SelectLayerByAttribute_management('lyr', 'ADD_TO_SELECTION', '"HydrID" = %s AND "TestDate" = (SELECT MAX("TestDate") FROM %s WHERE "HydrID" = %s)' % (ID, fc, ID))