I have a table (Feature class in FGDB) with 2 columns, one is the "name" and the other is a list of year values as text "Value1".

How do I obtain the lowest 5 values for EACH name?

I am trying to do this with arcpy or even select by attributes however I am lacking SQL skills to make this happen as expected.I tried this with arcpy however it comes down to SQL for this statement. I tried MIN(date) and that is only for one value.

arcpy.SelectbyAttribute_management(r"<my path>"), "New_Selection", ' [Field1] = 'lowest5' AND GROUPBY [NAME] 
  • 1
    Can you give us a screen shot of the table? What sort of feature class/table is it? Is it shapefile, personal GDB, file GDB etc.. can you show us where you're up to with arcpy? Commented Aug 16, 2017 at 22:12
  • I can't do a screenshot for security reasons, however it is in a FGDB.
    – Robert C
    Commented Aug 16, 2017 at 22:29
  • 1
    File geodatabases aren't real databases, they don't support the full SQL. If you're ok with python I would use a search cursor to get a list of unique names, then select by each unique name and search cursor again to get the year values as a list, sort the list smallest to largest and return the 5th value in the list to define the where clause for each unique name... where do you need to go from here? Commented Aug 16, 2017 at 22:35
  • 2
    There should be no need for SQL to do this. Try Summary Statistics to group and order, and cursors to read first 5 values in each group in the output table.
    – PolyGeo
    Commented Aug 16, 2017 at 22:39
  • 1
    Yup, this is an exercise for Summary Statistics
    – Vince
    Commented Aug 16, 2017 at 22:40

1 Answer 1


This is one way to do it:

def SelectLowestN(layer,n,groupfield,valuefield):
    import operator
    list1 = sorted([i for i in arcpy.da.SearchCursor(layer,[groupfield,'OID@',valuefield])],key=operator.itemgetter(0,2))
    for item in list1:
        if key not in d:
    for key in d:
    sql="{0} IN ({1})".format(arcpy.AddFieldDelimiters(layer,"OBJECTID"),", ".join(map(str,oids)))

Call the function:



enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.