AnsweredAssumed Answered

Trying to use arcpy find duplicates in layers from mxd.

Question asked by gis_student2 on Jun 12, 2018
Latest reply on Jun 14, 2018 by gis_student2

Basically i want to find duplicate records from my attribute tables in my mxd not my data base because my mxd. has several definition query's i would like to preserve. I am essentially trying to script this query  

[FIELD_NAME] In (SELECT [FIELD_NAME] FROM [TABLE_NAME] GROUP BY [FIELD_NAME] HAVING Count(*)>1 )

Any tips as to why this wont cooperate? I either get a cannot combine string and layer objects error or an invalid query error because it says my layer does not exist because it wont find the table related to my mxd. 

 

Cheers,  

 

# Set workspace environments. Workspace must be a connection file to an ADMIN (sde) connection
arcpy.env.workspace = r"\\fs\GIS\admin\temp_connect_files\COF_temp.sde"
arcpy.env.overwriteOutput = True
print "workspace environment connected"
# Set workspace variable
workspace = arcpy.env.workspace


# Local variables:

mxd = arcpy.mapping.MapDocument(r"P:\Joey_Plessis\service_cards\pdf_map_automation_script\DEFAULT_bc_one_call.mxd")


df = arcpy.mapping.ListDataFrames(mxd,"*")[0] #DONT WORRY ABOUT THIS PART


layer = arcpy.mapping.ListLayers(mxd, raw_input("enter layer name"), df)[0]


field = raw_input("enter field name")

print layer
print field
where = field +" " + "In" + " " + "(SELECT" +" "+ field + " "+ "FROM" +" " + "layer" + " "+ "GROUP BY" +" " + field +" "+ "HAVING Count(*)>1 )"
print where


arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION" ,where)

Outcomes