如何在sqldf查询中包含用户定义的变量?

时间:2018-06-18 19:48:14

标签: r sqldf

我有一个值列表,我想在将来解析。暂时确保我的查询格式正确。我有这个问题:

var2 = "Application Maintenance"

r = fn$sqldf("Select Iteration, (SUM(Hours) / SUM(Effort)) as Efficiency
     From df WHERE Iteration = $var2") 

query = sprintf('Select Iteration, (SUM(Hours) / SUM(Effort)) 
as Efficiency from df WHERE Iteration = %s', var2)

q = sqldf(query)

我在两次尝试中都收到错误:result_create中的错误(conn @ptr,statement):   接近"维护":语法错误

我过去只使用过一次sqldf,所以我还是新手。当我将$ var2更改为"应用程序维护"时,查询工作正常。但是当我使用用户定义的变量时,它不起作用。

我已经尝试过其他Stackoverflow帖子的解决方案,但我一直收到同样的错误。

非常感谢任何帮助!

编辑:列表迭代的代码(path_list是字符串列表):获取NULL ' tcltk :: as.tclObj(X)出错:无法处理模式对象' list''

for (var1 in path_list){
  query = fn$sqldf("Select Iteration, (SUM(Hours) / SUM(Effort)) as Efficiency 
  From df WHERE Iteration = '$var1'")
  print(query)

}

编辑2:path_list包含在这种格式中(点击RStudio中的列表时:

       Iteration
'Application Maintenance'
'Task'
'QA'

编辑3:包含前15行的示例。此数据集中有数千个。改变了#迭代'出于保密原因的列名:

There are ~15 unique 'Iteration' types, this captures first 15 rows

1 个答案:

答案 0 :(得分:1)

我们可以将$var包装在单引号中

library(sqldf)
fn$sqldf("Select Iteration, (SUM(Hours) / SUM(Effort)) as Efficiency
 From df WHERE Iteration = '$var2'") 

使用可重现的例子

fn$sqldf("select cyl, (sum(hp)/sum(wt)) as Efficiency from mtcars where new = '$var2'")
#    cyl Efficiency
#1   6   34.21462

如果我们在循环中这样做

path_list <- c("Mazda RX4", "Datsun 710", "Valiant")
for(var1 in path_list) print(sqldf(sprintf("select cyl, (sum(hp)/sum(wt)) as Efficiency from mtcars where new = '%s'", var1)))
#cyl Efficiency
#1   6   41.98473
#  cyl Efficiency
#1   4   40.08621
#  cyl Efficiency
#1   6   30.34682

数据

data(mtcars)
mtcars$new <- row.names(mtcars)
var2 <- "Hornet 4 Drive"