Я пытаюсь выполнить скрипт R (в частности, dplyr) в SQL как часть хранимой процедуры ETL. И код R, и запрос SQL выглядят успешными. Я полагаю, что проблема заключается в попытке манипулировать выводом. Я выдал следующую ошибку: «Неверный синтаксис рядом с«; ». Ожидается разговор»
Мой источник для этой структуры: https://docs.microsoft.com/en-us/sql/advanced-analytics/r/creating-workflows-that-use-r-in-sql-server?view=sql-server-ver15
EXECUTE sp_execute_external_script @language = N'R'
, @script = N'
rm(list = ls(all.names = TRUE))
library(dplyr)
library(tidyr)
library(rlang)
library(lubridate)
OutputDataSet <- InputDataSet %>% mutate(Date=as.Date(effective)) %>% select(-effective) %>%
group_by(rowno_dmddmhi_dmd) %>%
tidyr::complete(Date = seq.Date(min(Date), max(Date), by="day"))%>% fill(balance)%>%
mutate(amount = ifelse(is.na(amount), 0, amount))%>%mutate(opp_amount = amount * -1) %>%
arrange(rowno_dmddmhi_dmd,desc(Date)) %>% group_by(rowno_dmddmhi_dmd) %>%
mutate(cumsum = cumsum(opp_amount))%>%group_by(rowno_dmddmhi_dmd)%>%
mutate(balance.at.date=balance + cumsum)%>%ungroup() %>% mutate(Report_Date=as.Date(Date)-1)%>%select(rowno_dmddmhi_dmd,Date,balance.at.date,Report_date);'
, @input_data_1 = N'SELECT data_.rowno_dmddmhi_dmd,
dmd.balance,
Sum(data_.amount) AS amount,
data_.effective
FROM (
--Top False Cap
SELECT dmhi.rowno,
dmhi.rowno_dmddmhi_dmd,
0 AS amount,
CONVERT(Date, Getdate()) AS effective
FROM ks208.dbo.dmd
INNER JOIN dmon208.dbo.dmhi
ON dmd.rowno = dmhi.rowno_dmddmhi_dmd
UNION
SELECT dmhi.rowno,
dmhi.rowno_dmddmhi_dmd,
dmhi.amount,
CONVERT(Date, dmhi.effective) AS effective
FROM ks208.dbo.dmd
INNER JOIN ks208.dbo.dmhi
ON dmd.rowno = dmhi.rowno_dmddmhi_dmd
UNION
SELECT dmhi.rowno,
dmhi.rowno_dmddmhi_dmd,
dmhi.amount,
CONVERT(Date, dmhi.effective) AS effective
FROM dmon208.dbo.dmd
INNER JOIN dmon208.dbo.dmhi
ON dmd.rowno = dmhi.rowno_dmddmhi_dmd
-- False Bottom Cap
UNION
SELECT dmhi.rowno,
dmhi.rowno_dmddmhi_dmd,
0 AS amount,
CONVERT(Date, Dateadd(year, -4, Getdate())) AS effective
FROM ks208.dbo.dmd
INNER JOIN dmon208.dbo.dmhi
ON dmd.rowno = dmhi.rowno_dmddmhi_dmd) AS data_
INNER JOIN ks208.dbo.dmd
ON dmd.rowno = data_.rowno_dmddmhi_dmd
WHERE Year(data_.effective) >= Year(Getdate()) - 4
GROUP BY data_.rowno_dmddmhi_dmd,
data_.effective,
dmd.balance
'
with RESULT sets (("rowno_dmddmhi_dmd" int not null, "Date" Date not null, "balance.at.date" float not null, "Report_date" Date not null ));
end;