I am working on a script that is attempting to use ORDER BY. The results I'm seeing are unordered. I've dug quite a bit in GeoNet and elsewhere to no avail. My data is in a FGBD, my understanding is ORDER BY should work...
I'm hoping that there is something wrong with the logic in my script, and someone might help me identify it.
In a nutshell the script selects a group of features based on FEAT_SEQ, the selection is then sorted by a RANK field (NOT WORKING), the first row is then written to a new feature class. All of this is working except for the ordering. The output simply writes the first row, unsorted.
- The TableSelect, selects features in a FC by the FEAT_SEQ field. The FEAT_SEQ is a result of the identify duplicate tool.
- This selection is intended to be "sorted" by the ORDER BY using the SearchCursor
- Finally the first row is written to a new FC
Seq_Count <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="comment token">#counter for FEAT_SEQ selection</SPAN>
<SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">#loop - range 7631 (all)? for full run</SPAN>
Seq_Count <SPAN class="operator token">=</SPAN> Seq_Count <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="comment token"># adds 1 to Seq_Count</SPAN>
Where <SPAN class="operator token">=</SPAN> <SPAN class="string token">"FEAT_SEQ ="</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>Seq_Count<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#sets sql expreassion to Seq_Count #</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>"Looping Seq <SPAN class="comment token">#" + str(Seq_Count)) # prints Seq_Count # for ref. </SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>TableSelect_analysis<SPAN class="punctuation token">(</SPAN>DUPES<SPAN class="punctuation token">,</SPAN> Out<SPAN class="punctuation token">,</SPAN> Where<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># selects </SPAN>
<SPAN class="comment token">#print("Selection Complete - saved to cleanscript.gdb/SelSection") # loop done</SPAN>
<SPAN class="comment token">### load selection from previous output, evalute winner, save winner to winnigselection</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>Out<SPAN class="punctuation token">,</SPAN> field_names <SPAN class="operator token">=</SPAN> fieldnames<SPAN class="punctuation token">,</SPAN> sql_clause <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>None<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ORDER BY RANK'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> searchCursor<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">#orders search by ranked</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>InsertCursor<SPAN class="punctuation token">(</SPAN>winnerTable<SPAN class="punctuation token">,</SPAN> fieldnames<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> iCur<SPAN class="punctuation token">:</SPAN>
row <SPAN class="operator token">=</SPAN> next<SPAN class="punctuation token">(</SPAN>searchCursor<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#goes to first row (previouly ordered by rank)</SPAN>
<SPAN class="comment token">#print(row)</SPAN>
iCur<SPAN class="punctuation token">.</SPAN>insertRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#print("insert complete")</SPAN>
<SPAN class="comment token">#del iCur #seems like you do this for housekeeping </SPAN>
<SPAN class="comment token">#del searchCursor #seems like you do this for housekeeping</SPAN>
result <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetCount_management<SPAN class="punctuation token">(</SPAN>winnerTable<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#counts output to winnerTable</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'{} has {} records'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>winnerTable<SPAN class="punctuation token">,</SPAN> result<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#prints count</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>The sql_clause is on line 9 (shown below)
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>Out<SPAN class="punctuation token">,</SPAN> field_names <SPAN class="operator token">=</SPAN> fieldnames<SPAN class="punctuation token">,</SPAN> sql_clause <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>None<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ORDER BY RANK'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> searchCursor<SPAN class="punctuation token">:</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
screenshot of same code (in case it is easier to read)

Thanks in advance for any insights anyone has to offer!