Updating stats with group by instead of row iteration in PHP