连接表时r中的sqldf错误

时间:2017-08-24 17:14:41

标签: r sqldf

我有两个客户和订单表,我想过滤掉满足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

Table

step1&lt; - sqldf(&#34; select * from customer_table as ct inner join order_table as ot ON ct.customer_id = ot.customer_id              其中order_date&lt; 20161222和order_amount = 1                   group by ct.customer_id;&#34;)

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; ")

有人可以帮忙吗?谢谢

1 个答案:

答案 0 :(得分:1)

我无法复制任何错误。我对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")

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