sql - dplyr sql连接

考虑以下 SQL:

    SELECT D.product_name
      FROM business.payment P
 LEFT JOIN dim.product_name D 
        ON D.product_id = P.product_id

查询返回付款表中的 product_names 列表,并基于联接执行此操作。

如何在不拉入内存的情况下在 dplyr 中复制这样的东西?我正在使用数据库连接。

我尝试了以下方法,但无济于事:

product_name <- 
  business %>% 
  tbl('dim_product') 

business %>% 
  tbl('payment') %>% 
  left_join(product_name, by = 'product_id') %>% 
  select(product_name) %>% 
  collect()

我搜索了很多,似乎没有人解决这个问题。

谢谢!

最佳答案

这是事后的一段时间,但也许您仍在寻找或好奇基于 dplyr 动词的选项。我正在为我的工作处理同一个问题,并遇到了你(某种)未回答的问题。当我使用 DBIodbc 包针对 MSSQL 数据库运行它时,下面对我有用。

我在加入之前从表中选择了感兴趣的列,因为这通常是查询数据库时的最佳做法。 dplyr 连接函数默认会进行自然连接,因此您可能不必显式提供 by 参数。

db_con <- DBI::dbConnect(
  drv = odbc::odbc(),
  dsn = <data source name>
)

db_con %>%
  tbl("table1") %>%
  select(col1, col2, col3) %>%
  left_join(
    db_con %>% tbl("table2") %>% select(col3,  col4, col5)
  )

https://stackoverflow.com/questions/39864427/

相关文章:

python - 如何使用 python 从网站上抓取图表?

php - 使用带有表前缀的 DB::raw()

coldfusion - 如何从 Coldfusion 中的结构数组中删除重复项

sql-server - 为什么即使回滚事务,SQL Server 仍保持可变状态?

hibernate - Spring Boot/JPA/Hibernate,如何根据Spring配置

python - 如何删除 Tkinter OptionMenu 小部件的边框

java - 从测试用例调用 Controller 时,使用自动连线组件测试 Controller

python - 如何清除 PYQTGRAPH 中的 ScatterPlotItem

c - FreeRTOS 中的抢占

curl - 如何使用 curl 访问 IBM speech-to-text api?