Query the row with the greatest/least value per group