2

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?

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
Sharath
  • 2,348
  • 8
  • 45
  • 81

1 Answers1

1
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).

Scarabee
  • 5,437
  • 5
  • 29
  • 55