[英]Create date variable for dbGetQuery in R?
basically this is the start of my query/code:基本上这是我的查询/代码的开始:
dbGetQuery(con,'SELECT user_id, unique_id
where order_date > parse_datetime('2021-11-01')')
(took out a load because this gets the point across). (拿出一个负载,因为这让我们明白了)。
The query is quite long and uses the date from above a good few times.查询很长,并且多次使用上面的日期。 So I was wondering if I can create a variable and replace the date.
所以我想知道是否可以创建一个变量并替换日期。
Something in the format:格式的东西:
date_variable <- 2021-11-01
dbGetQuery(con,'SELECT user_id, unique_id
where order_date > parse_datetime('date_variable')')
I am very new to R and don't really know what I'm doing so any help would be appreciated!我对 R 很陌生,我真的不知道我在做什么,所以任何帮助将不胜感激!
Thanks!谢谢!
Yes, you can.是的你可以。 I prefer to use
glue
package for this:我更喜欢为此使用
glue
package:
library(glue)
library(DBI)
date_variable <- as.Date("2021-11-01")
query <- glue_sql("SELECT user_id, unique_id where order_date > parse_datetime({date_variable})",
date_variable = date_variable,
.con = con)
dbGetQuery(con, query)
This source can be interesting for you: Run Queries Safely这个来源对你来说可能很有趣:安全运行查询
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.