Fixed aggregation query (tested it thorougly on actual data)