Change `having` to `where` for better database compatibility