SQL 通过 dbgetQuery 使用 CASE WHEN 查询不起作用
SQL query with CASE WHEN by dbgetQuery does not work
我有一个问题:我在 R 中测试了 CASE WHEN SQL 查询,但我得到了一个错误。能否请您提出解决方案?
这里是重现的代码:
library(sparklyr)
library(gapminder)
spark_conn <- spark_connect(master = "local")
data("gapminder")
copy_to(spark_conn, gapminder)
src_tbls(spark_conn)
#The query gave an error
DBI::dbGetQuery(spark_conn, "
SELECT *
CASE WHEN pop > 100000000 THEN 'Biggy'
WHEN pop < 10000000 THEN 'Small'
ELSE 'Medium' END
AS pop_category
FROM gapminder
WHERE year = 1957
")
Error: org.apache.spark.sql.catalyst.parser.ParseException:
mismatched input 'WHEN' expecting <EOF>(line 3, pos 21)
== SQL ==
SELECT *
CASE WHEN pop > 10000000 THEN 'Biggy'
---------------------^^^
WHEN pop < 10000000 THEN 'Small'
ELSE 'Medium' END
AS pop_category
FROM gapminder
*
后缺少逗号 ,
。只需在它后面添加逗号,您的查询就可以正常工作
SELECT *,
CASE WHEN pop > 100000000 THEN 'Biggy'
WHEN pop < 10000000 THEN 'Small'
ELSE 'Medium' END
AS pop_category
FROM gapminder
WHERE year = 1957
我有一个问题:我在 R 中测试了 CASE WHEN SQL 查询,但我得到了一个错误。能否请您提出解决方案?
这里是重现的代码:
library(sparklyr)
library(gapminder)
spark_conn <- spark_connect(master = "local")
data("gapminder")
copy_to(spark_conn, gapminder)
src_tbls(spark_conn)
#The query gave an error
DBI::dbGetQuery(spark_conn, "
SELECT *
CASE WHEN pop > 100000000 THEN 'Biggy'
WHEN pop < 10000000 THEN 'Small'
ELSE 'Medium' END
AS pop_category
FROM gapminder
WHERE year = 1957
")
Error: org.apache.spark.sql.catalyst.parser.ParseException:
mismatched input 'WHEN' expecting <EOF>(line 3, pos 21)
== SQL ==
SELECT *
CASE WHEN pop > 10000000 THEN 'Biggy'
---------------------^^^
WHEN pop < 10000000 THEN 'Small'
ELSE 'Medium' END
AS pop_category
FROM gapminder
*
后缺少逗号 ,
。只需在它后面添加逗号,您的查询就可以正常工作
SELECT *,
CASE WHEN pop > 100000000 THEN 'Biggy'
WHEN pop < 10000000 THEN 'Small'
ELSE 'Medium' END
AS pop_category
FROM gapminder
WHERE year = 1957