Fixed sql syntax errors whilte converting the cloud query to a single SQL query.