Handle aggregate queries correctly