rsqlite_send_query中的错误(conn @ptr,statement):接近“(”:语法错误_____

时间:2018-04-04 23:17:49

标签: mysql sql r sqldf

我得到这个错误,而我必须将行转置到列

library(sqldf)
sqldf("SELECT id,name,ROW_NUMBER() over (PARTITION BY id order by id) as row_no FROM dd")
  

rsqlite_send_query中的错误(conn @ptr,statement):near“(”:syntax   错误_____

我应该怎么做或者请给我一个替代方案,因为我想在shiny应用中使用它

2 个答案:

答案 0 :(得分:1)

假设我们有这个:

library(sqldf)
dd <- data.frame(id = c(1, 2, 3), name = c("A", "B", "C"), X = 1:9)

然后这三个都给出了相同的结果:

# 1. PostgreSQL - supports over/partition

library(RPostgreSQL)
sqldf("SELECT id, name, \"X\", ROW_NUMBER() over (PARTITION BY id order by id) as row_no 
       FROM dd order by id")

# 2. SQLite - does not support over/partition, use join instead

# Ensure RPostgreSQL is NOT loaded to force use of SQLite.
sqldf("select a.*, count(*) row_no 
       from dd a join dd b on a.id = b.id and b.rowid <= a.rowid
       group by a.rowid
       order by a.rowid")

# 3. Base R

transform(dd, row_no = ave(1:nrow(dd), id, FUN = seq_along))

答案 1 :(得分:0)

sqlite不支持分区。

您可以查看this link