使用 dplyr/dbplyr 添加 postgres 时间间隔

Add postgres time interval using dplyr/dbplyr

我在 R 中有一个数据库连接,想在 Postgres 中使用 dplyr (v0.5):

实现以下过滤步骤
WHERE time1 < time2 - INTERVAL '30 minutes'

(参见 https://www.postgresql.org/docs/9.1/static/functions-datetime.html

我尝试了以下操作(这是我对 POSIX 对象所做的)但收到此错误:

tbl(con, 'data') %>%
  filter(time1 < time2 - 30 * 60) %>%
  collect()
# ERROR: operator does not exist: timestamp without timezone - numeric

正确的做法是什么?

根据 SQL translation vignette:

Any function that dplyr doesn’t know how to convert is left as is. This means that database functions that are not covered by dplyr can be used directly via translate_sql().

但是你不能使用 %interval%,因为你的条件在 R:

中在句法上是不正确的
time1 < time2 - %interval% "30 minutes"
# Error: unexpected SPECIAL in "time1 < time2 - %interval%"

使用interval不是更好:

time1 < time2 - interval "30 minutes"
# Error: unexpected string constant in "time1 < time2 - interval "30 minutes""

但以下技巧确实有效:

dbplyr::translate_sql(time1 < time2 %- interval% "30 minutes")
# <SQL> "time1" < "time2" - INTERVAL '30 minutes'

因此这段代码应该可以回答您的问题:

tbl(con, "data") %>%
  filter(time1 < time2 %- interval% "30 minutes") %>%
  collect