使用DBI Package连接到PostgreSQL时,R区别

时间:2017-09-26 10:27:41

标签: r postgresql dbplyr

以下代码打印:

SELECT "district_code" FROM sd_stage.table1 GROUP BY "district_code"

但我期待:

select distinct(district_code) from sd_stage.table1

代码:

library(DBI)
library(tidyverse)
library(dbplyr)

conn_obj <- DBI::dbConnect(RPostgreSQL::PostgreSQL(), 
                           host = "127.0.0.1",
                           user = "testingdb",
                           password = "admin@123")
on.exit(DBI::dbDisconnect(conn_obj))

tbl_oil_root_segment <- dplyr::tbl(conn_obj, 
       dbplyr::in_schema('sd_stage','table1'))

tbl_oil_root_segment %>% distinct(oil_district) %>% show_query()

输出正确但生成的查询似乎不是100%。那么无论如何我可以实现查询吗?

1 个答案:

答案 0 :(得分:1)

tbl_oil_root_segment %>% select(oil_district) %>% distinct %>% show_query()

将创建您期望的查询。

但请注意,在SQL select distinct a from t中与select a from t group by a相同(请参阅this question)。