如何使用单引号和双引号编写查询时避免R错误

时间:2018-03-15 13:17:22

标签: r r-dbi

在R中编写Query以从Presto数据库中选择数据时遇到问题。 我用下一个代码

    library(DBI)
    library(dplyr)
    library(RPresto)
    res = dbSendQuery(con, " select RESPONDENT as Respondent,RESPONSE_DATE 
    as Date,
    MAX(CASE
    WHEN QUESTION_ID = '18' AND RESPONSE_ID ='5' THEN 'Very Satisfied'
    WHEN QUESTION_ID = '18' AND RESPONSE_ID ='4' THEN 'Satisfied'
    WHEN QUESTION_ID = '18' AND RESPONSE_ID ='3' THEN 'Neutral'
    WHEN QUESTION_ID = '18' AND RESPONSE_ID ='2' THEN 'Dissatisfied'
    WHEN QUESTION_ID = '18' AND RESPONSE_ID ='1' THEN 'Very Dissatisfied' 
    ELSE NULL END) AS "How was our service looks based on your last meal?"
    from surveytable
    group by 1,2")

错误出现在R喜欢

    Error: unexpected numeric constant in:
    "                WHEN QUESTION_ID = '18' AND RESPONSE_ID ='2' THEN 
    'Dissatisfied' 
    WHEN QUESTION_ID = '18' AND RESPONSE_ID ='1' THEN 'Very Dissatisfied' 
    ELSE NULL END) AS "7."
    >                 from dev_bi.Checkmarket_survey_78995
    Error: unexpected symbol in "                from 
    dev_bi.Checkmarket_survey_78995"
    >                 group by 1,2
    Error: unexpected symbol in "                group by"
    >                 limit 1")
    Error: unexpected numeric constant in "                limit 1"

1 个答案:

答案 0 :(得分:1)

您可以使用反斜杠在查询中添加每个双引号。见:

Item1 Item2 Item3 Item4
F     G     H     I
A     H     D     R