Fixed stats SQL query