-
Notifications
You must be signed in to change notification settings - Fork 5
/
run_query.R
33 lines (26 loc) · 1.14 KB
/
run_query.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
run_query <- function(server_name = "server",
db = "db",
start_date = Sys.Date() - 91, # rolling 13 weeks (ish)
end_date = Sys.Date(),
schema_name = "schema",
tablename = "table",
date_var = "date_co1",
col_vars = c(
"date_col1",
"col1",
"col2",
"col3")) {
con <- make_connection(server_name,db)
# internal wrapper function that make a regular SQL Server connection
# this function uses the 'server_name' and 'db' variables from the configuration file
start_date <- as.Date(start_date)
end_date <- as.Date(end_date)
query <- glue_sql(.con = con,
"SELECT {`col_vars`*},
'{`schema_name`}.{`tablename`}' AS 'tablename'
FROM {`schema_name`}.{`tablename`} with(nolock)
WHERE CAST({`schema_name`}.{`tablename`}.{`date_var`} AS DATE) BETWEEN {start_date} AND {end_date}")
res <- DBI::dbGetQuery(con, query)
dbDisconnect(con)
return(res)
}