连接表时 r 中的 sqldf 错误

sqldf error in r when joining tables

我有customer和order两张表,我想过滤掉满足step1和step2需求的customer_id,而step2.5时控制台显示

Error: Cannot pass NA to dbQuoteIdentifier()
In addition: Warning message:
In field_types[] <- field_types[names(data)] :
  number of items to replace is not a multiple of replacement length

step1<- sqldf("select * from customer_table as ct inner join order_table as ot ON ct.customer_id=ot.customer_id 其中 order_date<20161222 且 order_amount=1 按 ct.customer_id;")

分组
step2<- sqldf("select ot.customer_id from  order_table as ot 
              where order_date between 20161222 and 20170222
              and order_amount=0
              group by ot.customer_id;")

step2.5<- sqldf("select * from step1 as s1 inner join step2 as s2 on s1.customer_id=s2.customer_id; ")

有人可以帮忙吗?谢谢

我无法复制任何错误。我对 SQL 做了一些改进,但如果这不能解决您的问题,请在您的问题中以可重现的格式提供您的数据。

data(iris)
customer_table <- iris
order_table    <- iris

customer_table$customer_id <- 1:nrow(iris)
order_table$customer_id    <- 1:nrow(iris)

customer_table$order_amount <- 1
order_table$order_amount    <- 0
order_table$order_date      <- rep(c(20161221, 20161223))

step1 <- sqldf("select ct.* 
                from customer_table ct 
                join order_table ot on 
                ct.customer_id=ot.customer_id 
                where ot.order_date < 20161222 
                and ct.order_amount=1
                group by ct.customer_id")

step2 <- sqldf("select customer_id 
                from order_table 
                where order_date 
                between 20161222 and 20170222
                and order_amount=0
                group by customer_id")

step2.5 <- sqldf("select * from step1 s1 
                 join step2 s2 
                 on s1.customer_id=s2.customer_id")

这个概念证明创建了一个 table 没有错误。 table 使用此示例数据正确地包含 0 行。