Search code examples
filemaker

Find max value in a group in FileMaker


How to select only max values in a group in the following set

id  productid  price  year
---------------------------
1   11         0,10   2015
2   11         0,12   2016
3   11         0,11   2017
4   22         0,08   2016
5   33         0,02   2016
6   33         0,01   2017

Expected result for each productid and max year would be

id  productid  price  year
---------------------------
3   11         0,11   2017
4   22         0,08   2016
6   33         0,01   2017

Solution

  • This works for me.

    ExecuteSQL (
    "SELECT t.id, t.productid, t.price, t.\"year\"
        FROM test t
        WHERE \"year\" = 
            (SELECT MAX(\"year\") FROM test tt WHERE t.productid = tt.productid)"
    ; "  " ; "")
    

    Adapted from this answer: https://stackoverflow.com/a/21310671/832407