Вы можете разбить df на строки, создать запрос для каждого из них, свернуть запросы с помощью 'UNION ALL \ n', а затем получить:
library(RSQLite)
library(DBI)
library(tidyverse)
library(glue)
#>
#> Attaching package: 'glue'
#> The following object is masked from 'package:dplyr':
#>
#> collapse
t1 <- tibble(date = c("5/22", "5/22", "9/9", "10/10"),
program = c("E7", "H9", "E7", "R8"),
name = c("Square", "Circle", "Circle", "Triangle"),
type = c("angle", "smooth", "smooth", "angle"),
height = c(5, 4, 7, 10),
width = c(5, 4, 5, 5))
t2 <- tibble(date = c("5/22", "5/22", "9/9", "10/10"),
program = c("E7", "H9", "E7", "R8"),
name = c("Square", "Circle", "Circle", "Triangle"),
val1 = c(5, 10, 3.2, 999),
val2 = c(2.4, 43, 9, 1))
t3 <- tibble(type = c("angle", "smooth"),
one = c("a", "b"),
two = c("g", "c"),
three = c("h", "d"))
df <- tibble(date = c("5/22", "9/9", "10/10"),
program = c("E7", "E7", "R8"),
name = c("Square", "Circle", "Triangle"))
con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "tab1", t1)
dbWriteTable(con, "tab2", t2)
dbWriteTable(con, "tab3", t3)
queries <- df %>%
split(seq_len(nrow(.))) %>%
map(~{
d <- .x$date
p <- .x$program
n <- .x$name
glue('SELECT t1.date,
t1.program,
t1.name,
t1.type,
t2.val1,
t2.val2,
t3.one,
t3.two,
t3.three
FROM tab1 t1
LEFT JOIN tab2 t2 ON t1.date = t2.date AND t1.program = t2.program
LEFT JOIN tab3 t3 ON t1.type = t3.type
WHERE t1.date = "{d}"
AND t1.program = "{p}"
AND t1.name = "{n}"')
})
q <- paste(queries, collapse = "UNION ALL\n")
dbGetQuery(con, q)
#> date program name type val1 val2 one two three
#> 1 5/22 E7 Square angle 5.0 2.4 a g h
#> 2 9/9 E7 Circle smooth 3.2 9.0 b c d
#> 3 10/10 R8 Triangle angle 999.0 1.0 a g h
Создано в 2020-04-03 представьте пакет (v0.3.0)