import arcpy fc = "D:\\Projects\\proj1\\SumsOfRoutes.gdb\\all_routes_1stBatch" field = "r_Identity" field2 = "Shape_Length" ##sql = "(None, 'GROUP BY \"r_Identity\"')" sql = "(None,'Group BY r_Identity')" fields=["*"] cursor = arcpy.da.SearchCursor(fc,fields,sql_clause=sql) # print r_Identity and Shape_Length of each feature in feature class for row in cursor: print(str(int(row.getValue(field))) + ": " + str(row.getValue(field2)) + " meters")
Solved! Go to Solution.
fc = r'H:\Documents\ArcGIS\Default.gdb\RainGauges' fields = ('Data_Type', 'ObjectID') sql=[None, "GROUP BY Data_Type, ObjectID"] cursor = arcpy.da.SearchCursor(fc, fields,sql_clause=sql) for row in cursor: print row[0]
You must list all fields that will be included in the sql_clause parameter in the fields parameter.fc = r'H:\Documents\ArcGIS\Default.gdb\RainGauges' fields = ('Data_Type', 'ObjectID') sql=[None, "GROUP BY Data_Type, ObjectID"] cursor = arcpy.da.SearchCursor(fc, fields,sql_clause=sql) for row in cursor: print row[0]
You cannot include Shape_Length (see bold above) in your select because it is not included in your GROUP BY portion. Remove it and see if it at least completes without error.
import arcpy fc = "D:\\Projects\\proj1\\SumsOfRoutes.gdb\\all_routes_1stBatch" field1 = "r_Identity" ##field2 = "Shape_Length" sql = [None,"Group BY r_Identity, ObjectID"] fields=[field1] cursor = arcpy.da.SearchCursor(fc,fields,sql_clause=sql) # print r_Identity and Shape_Length of each feature in feature class for row in cursor: print str(row)
OK, THANKS I GET AN ANSWER BY RUNNING THE BELOW TEXT:import arcpy fc = "D:\\Projects\\proj1\\SumsOfRoutes.gdb\\all_routes_1stBatch" field1 = "r_Identity" ##field2 = "Shape_Length" sql = [None,"Group BY r_Identity, ObjectID"] fields=[field1] cursor = arcpy.da.SearchCursor(fc,fields,sql_clause=sql) # print r_Identity and Shape_Length of each feature in feature class for row in cursor: print str(row)
Only I don't understand the thing with the OBJECTID,
and more so, this constellation DOES NOT HELP ME, AS I CAN'T REACH THE OTHER FIELDS OF THE FEATURES.
substitute field1 with "*" brings back the runtime error
You cannot use '*' when using sql_clause. You must specify each field explicitly.
The OBJECTID should not be required, if it ever was it is a bug and is fixed in 10.2.1.