简体   繁体   中英

Distinct in R while connecting to PostgreSQL using DBI Package

The below code prints:

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

but I am expecting:

select distinct(district_code) from sd_stage.table1

Code:

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()

Output is correct but the query which is generated seems to be not 100%. So is there anyway I can implement the query?

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

will create the query you expect.

However, note that in SQL select distinct a from t is the same as select a from t group by a (see this question ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM