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') sql=[None, "GROUP BY Data_Type, ObjectID"] cursor = arcpy.da.SearchCursor(fc, fields,sql_clause=sql) for row in cursor: print row[0]
sql = (None,'GROUP BY r_Identity')
fields=[field,field2]
sql = (None,'GROUP BY r_Identity')
sql = [None,"GROUP BY r_Identity"]
sql = (None,'GROUP BY r_Identity')I get the following error:
TypeError: 'where_clause' is not a string, so that is why I have the quotation marks. Any Ideas?
fields=["*"]
fc = r'H:\Documents\ArcGIS\Default.gdb\RainGauges' fields = ('Data_Type') sql=[None, "GROUP BY Data_Type, ObjectID"] cursor = arcpy.da.SearchCursor(fc, fields,sql_clause=sql) for row in cursor: print row[0]
import arcpy fc = "D:\\Projects\\proj1\\SumsOfRoutes.gdb\\all_routes_1stBatch" field1 = "r_Identity" field2 = "Shape_Length" sql = [None,'Group BY r_Identity'] fields=[field1,field2] 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)
RuntimeError: An invalid SQL stament was used. [SELECT r_Identity, Shape_Length, OBJECTID FROM all_routes_1stBatch GROUP BY r_Identity]
OK, James,
I tried to rewrite the script by your 2 suggestions:import arcpy fc = "D:\\Projects\\proj1\\SumsOfRoutes.gdb\\all_routes_1stBatch" field1 = "r_Identity" field2 = "Shape_Length" sql = [None,'Group BY r_Identity'] fields=[field1,field2] 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)
I get the following error:RuntimeError: An invalid SQL stament was used. [SELECT r_Identity, Shape_Length, OBJECTID FROM all_routes_1stBatch GROUP BY r_Identity]
Any Ideas as to what's wrong?
and the question I asked Joshua: I know listing only 2 fields will increase the efficiency of the cursor, but will it enable me to use other fields in the layer later on?
Update:
This must be a bug or something because I don't see how this can be correct. According to Shaun's reply in this thread: http://forums.arcgis.com/threads/77876-Group-By-functionality-of-da.cursors-sql_clause the suggestion is to include the "ObjectID" field in the group by clause to overcome the error you are experiencing. Here's my working code:fc = r'H:\Documents\ArcGIS\Default.gdb\RainGauges' fields = ('Data_Type') sql=[None, "GROUP BY Data_Type, ObjectID"] cursor = arcpy.da.SearchCursor(fc, fields,sql_clause=sql) for row in cursor: print row[0]
HOWEVER, this completely defeats the ability to actually group rows because the ObjectID is a unique value!! lol... Maybe I am simply misunderstanding here, but that is exactly what is happening in my test data. That is, it doesn't matter if real/actual groups exist in my select fields, they will NEVER group correctly because I must include the ObjectID in the group by clause (it is unique values).
I am happy to be corrected on this one!
I get the same runtimeerror as before... 😞