More

Updating numerical column with autonumbers in sorted query table in ArcGIS for Desktop?

Updating numerical column with autonumbers in sorted query table in ArcGIS for Desktop?


I am trying to find a suitable code that lets me to update a numerical column (namely, "RevOdrNo") of a sorted query table (not a separate table) with autonumbers (e.g. 1,2,3,4,5,6) in ArcGIS 10.3's Field Calculator without using the physical IDs (=not sorted). I sorted this query table by right clicking the heading of the other numerical column (namely, "AX_Co") and clicking "Sort Ascending (or Descending)".

I have had a look at a number of the websites including the following link, but none of them seems to work for the sorted query table because they rely on the 'physical' order of the table rows.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000004s000000.htm (See the section of "Accumulative and sequential calculations")

I have had a thought about using the Sort tool in ArcGIS, but it asks for the output table name, which means that the query table will become a separate table, which I don't want to see.

Does anyone have any ideas of how to do it?


If you don't want to have another table outputted fromSort, then I don't believe it will be easy (if possible) with Field Calculator. Your best bet is to use cursors, which have a parameter, sql_clause, for sorting:

tableName = "table" sortField = "AX_Co" sortType = "ASC" # use 'DESC' for descending incrField = "RevOdrNo" s = 1 # this will be your starting value -- change it to whatever you want fields = [sortField, incrField] sql_clause = (None, "ORDER BY {} {}".format(sortField, sortType)) with arcpy.da.UpdateCursor(tableName, fields, sql_clause=sql_clause) as cursor: for i,row in enumerate(cursor, s): row[1] = i cursor.updateRow(row)