使用 dbplyr 创建 SQL 查询时出现问题
Problem using dbplyr to create a SQL query
我正在尝试创建一个查询来清理 SQL 中的数据,但我不知道 SQL。为此,我创建了一个数据集样本并使用 dplyr
来清理和创建我想要的最终数据,然后我想使用 dbplyr
来创建查询。但是我在创建查询时遇到了问题
data <- read_csv("DataBase/raw/bquxjob_1016f9df_180d80c269e.csv")
#Creating query I will run in SQL
data %>%
filter(codigo_natureza_juridica == 2046
| codigo_natureza_juridica == 2054
| codigo_natureza_juridica == 2062
| codigo_natureza_juridica == 2070
| codigo_natureza_juridica == 2089
| codigo_natureza_juridica == 2097
| codigo_natureza_juridica == 2135
| codigo_natureza_juridica == 2232
| codigo_natureza_juridica == 2240
| codigo_natureza_juridica == 2259
| codigo_natureza_juridica == 2267 &
data_inicio_atividade <= '2011-12-31') %>%
mutate(year_open = format(data_inicio_atividade, format ="%Y"),
month_open = format(data_inicio_atividade, format ="%m"),
date_open = format(data_inicio_atividade, format ="%Y%m"),
year_close = ifelse(situacao_cadastral != 2,
format(data_situacao_cadastral, format ="%Y"),
NA),
month_close = ifelse(situacao_cadastral != 2,
format(data_situacao_cadastral, format ="%m"),
NA),
date_close = ifelse(situacao_cadastral != 2,
format(data_situacao_cadastral, format ="%Y%m"),
NA),
open_2007 = ifelse(year_open <= 2007, 1,0)) %>%
show_query()
此代码出现以下错误:
Error in UseMethod("show_query") :
no applicable method for 'show_query' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"
show_query()
仅适用于数据库,而您正试图在数据帧上使用它。要将数据从 csv 发送到临时数据库对象以创建查询,您可以使用 tbl_memdb()
而不是:
data %>%
tbl_memdb() %>%
filter(...) %>%
mutate(...) %>%
show_query()
我正在尝试创建一个查询来清理 SQL 中的数据,但我不知道 SQL。为此,我创建了一个数据集样本并使用 dplyr
来清理和创建我想要的最终数据,然后我想使用 dbplyr
来创建查询。但是我在创建查询时遇到了问题
data <- read_csv("DataBase/raw/bquxjob_1016f9df_180d80c269e.csv")
#Creating query I will run in SQL
data %>%
filter(codigo_natureza_juridica == 2046
| codigo_natureza_juridica == 2054
| codigo_natureza_juridica == 2062
| codigo_natureza_juridica == 2070
| codigo_natureza_juridica == 2089
| codigo_natureza_juridica == 2097
| codigo_natureza_juridica == 2135
| codigo_natureza_juridica == 2232
| codigo_natureza_juridica == 2240
| codigo_natureza_juridica == 2259
| codigo_natureza_juridica == 2267 &
data_inicio_atividade <= '2011-12-31') %>%
mutate(year_open = format(data_inicio_atividade, format ="%Y"),
month_open = format(data_inicio_atividade, format ="%m"),
date_open = format(data_inicio_atividade, format ="%Y%m"),
year_close = ifelse(situacao_cadastral != 2,
format(data_situacao_cadastral, format ="%Y"),
NA),
month_close = ifelse(situacao_cadastral != 2,
format(data_situacao_cadastral, format ="%m"),
NA),
date_close = ifelse(situacao_cadastral != 2,
format(data_situacao_cadastral, format ="%Y%m"),
NA),
open_2007 = ifelse(year_open <= 2007, 1,0)) %>%
show_query()
此代码出现以下错误:
Error in UseMethod("show_query") :
no applicable method for 'show_query' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"
show_query()
仅适用于数据库,而您正试图在数据帧上使用它。要将数据从 csv 发送到临时数据库对象以创建查询,您可以使用 tbl_memdb()
而不是:
data %>%
tbl_memdb() %>%
filter(...) %>%
mutate(...) %>%
show_query()