gpt4 book ai didi

r - 使用 DBI 包连接到 PostgreSQL 时在 R 中不同

转载 作者:行者123 更新时间:2023-11-29 12:17:19 25 4
gpt4 key购买 nike

下面的代码打印:

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%。那么无论如何我都可以实现查询吗?

最佳答案

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

将创建您期望的查询。

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

关于r - 使用 DBI 包连接到 PostgreSQL 时在 R 中不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46423954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com