Dbplyr error : Arithmetic overflow error converting expression to data type int

Dbplyr error : Arithmetic overflow error converting expression to data type int

我很确定已经有人问过类似的问题。然而,我的问题是特定于我将 R 会话连接到我的 SQL 服务器数据库时。

accept<-x%>% 

  group_by(SiteID, MachineID, LocationID) %>%

  filter(DateTime>="2019-01-1" & DateTime<"2019-12-31") %>%

  summarise(n=(sum(TenSecCount))) %>%

  collect()

当我尝试将数据收集到数据框中时,出现以下错误 --

Arithmetic overflow error converting expression to data type int.
'SELECT "SiteID", "MachineID", "LocationID", CAST("n" AS VARCHAR(MAX)) AS "n"
FROM (SELECT TOP 100 PERCENT "SiteID", "MachineID", "LocationID", (SUM("TenSecCount")) AS "n"
FROM (SELECT TOP 100 PERCENT *
FROM (SELECT TOP 100 PERCENT "MachineID", "OutletID", "TenSecCount"

有什么解决方法吗?

当您将数据收集到 R 中时会出现此错误,因为只有在收集时才会评估您的远程 SQL table。

dbplyr 通过 SQL 查询定义您的远程 table,这将 return 您的结果。在您要求 return 编辑结果之前,您的远程 table 定义与等待 运行.

的 SQL 脚本没有太大区别

当您从 table 请求结果时,在您使用 collect 的情况下,sql 代码在服务器上执行,结果 returned 到 R . 这意味着你可以有一个无效的远程 table 定义,并且在你执行它之前不知道它。例如:

remote_table <- server_df %>% 
  group_by(SiteID, MachineID, LocationID) %>%
  filter(DateTime>="2019-01-1" & DateTime<"2019-12-31") %>%
  summarise(n=(sum(TenSecCount)))
# no error because all we have done is define an sql query and store it in remote_table

# review underlying sql query
show_query(remote_table)
# if you copy & paste this query and try to run it directly on the server it will error

# attempt to collect data
local_table <- remote_table %>% collect()
# error occurs on evaluation

你可以判断出当 sql 被评估时出现的错误,因为 R returned 给了你导致错误的 sql 代码和 sql 错误信息. "Arithmetic overflow error converting expression to data type int." 是 SQL 错误,不是 R 错误。请参阅 this question 了解解决方法。

提示,大概是这样的:

remote_table <- server_df %>% 
  mutate(TenSecCount = CAST(TenSecCount AS BIGINT)) %>% # additional step changing data type
  group_by(SiteID, MachineID, LocationID) %>%
  filter(DateTime>="2019-01-1" & DateTime<"2019-12-31") %>%
  summarise(n=(sum(TenSecCount)))