从2个数据库中获取数据从SQL服务器到R

时间:2017-12-04 07:46:59

标签: r sql-server-2008 qsqlquery

我一直在尝试将SQL服务器从多个数据库链接到R,但我收到以下错误

42000 156 [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'as'.
42000 102 [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'B'.
42000 102 [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'C'.
[RODBC] ERROR: Could not SQLExecDirect

我正在尝试以下查询

dbConnect <- odbcDriverConnect('driver={SQL Server};server=server1;database=MarketData;database=BusinessData;trusted_connection=true')
Daily_Date <- sqlQuery(dbConnect,paste('select A.BusinessDate, MAX(B.RecordDate) as LastDividendDate,
+ MIN(C.RecordDate) as NextDividendDate from
+ (select  BusinessDate, SUM(Quantity) QOL
+ from BusinessData..TradeData 
+ where ID = 1729903 and BusinessDate >= as.Date("2016-02-24")
+ group by BusinessDate) A
+ left outer join
+ (SELECT RecordDate
+ FROM MarketData..DivData
+ where ID = 1729903) B
+ on A.BusinessDate >= B.RecordDate
+ left outer join
+ (select RecordDate from MarketData..DivData
+ where InstrumentID = 1729903) C
+ on A.BusinessDate <= C.RecordDate
+ group by A.BusinessDate
+ order by A.BusinessDate'))

相同的查询在SQL服务器中工作但在R中不起作用 有什么建议吗?

0 个答案:

没有答案