Applying aggregation functions to groups