Corrected aggregation query