我试图在下面代码的倒数第二行显示两个日期,然后是R中的SQL查询。无论如何这可能吗?
myConn <- odbcDriverConnect(connection = "driver={SQL Server};server=SLSQL398_AGLIS\\SLSQL398;database=PortfolioAnalytics;trusted_connection=true")
data<- sqlFetch(myConn, "Data_Factset.FundChar", colnames = TRUE)
SQLCommand<- data.frame(sqlQuery(myConn, "SELECT fc.[FactSet Fund Code], fc.FactsetDate, fc.[Asset Class], fc.[Fund Manager], d.Id AS DeskID, d.[Name] AS DeskName, c.ClosingDate, m.NumericGrossPerformanceTarget, fc.[% Factor Risk], fc.[Predicted Tracking Error], fc.[Portfolio Predicted Beta], fc.[Port. Ending Active Share]
FROM [PortfolioAnalytics].[Data_Factset].[FundChar] fc
LEFT OUTER JOIN Data_Axioma.FactSetAccount fsa ON fc.FactsetAccountCode = REPLACE(fsa.Code, '_', '')
LEFT OUTER JOIN dbo.Portfolio p ON fsa.PortfolioId = p.Id
LEFT OUTER JOIN dbo.Mandate m ON p.UnderlyingId = m.id
INNER JOIN dbo.Mandate c ON p.UnderlyingId = c.id
LEFT OUTER JOIN dbo.Desk d ON m.DeskId = d.Id
WHERE IncludesCash = 1
AND [FactSet Fund Code] IS NOT NULL
AND [Fund Manager] IS NOT NULL
AND [FactSet Fund Code] <> 'TSNA_US'
AND [Asset Class] IS NOT NULL
AND FactsetDate IN ('20180531', '20180228')
ORDER BY [Predicted Tracking Error] DESC"))
这是我的尝试,但是我收到了意外的符号错误:
d<-20180531
d2<-20180228
myConn <- odbcDriverConnect(connection = "driver={SQL Server};server=SLSQL398_AGLIS\\SLSQL398;database=PortfolioAnalytics;trusted_connection=true")
data<- sqlFetch(myConn, "Data_Factset.FundChar", colnames = TRUE)
SQLCommand<- data.frame(sqlQuery(myConn, "SELECT fc.[FactSet Fund Code], fc.FactsetDate, fc.[Asset Class], fc.[Fund Manager], d.Id AS DeskID, d.[Name] AS DeskName, c.ClosingDate, m.NumericGrossPerformanceTarget, fc.[% Factor Risk], fc.[Predicted Tracking Error], fc.[Portfolio Predicted Beta], fc.[Port. Ending Active Share]
FROM [PortfolioAnalytics].[Data_Factset].[FundChar] fc
LEFT OUTER JOIN Data_Axioma.FactSetAccount fsa ON fc.FactsetAccountCode = REPLACE(fsa.Code, '_', '')
LEFT OUTER JOIN dbo.Portfolio p ON fsa.PortfolioId = p.Id
LEFT OUTER JOIN dbo.Mandate m ON p.UnderlyingId = m.id
INNER JOIN dbo.Mandate c ON p.UnderlyingId = c.id
LEFT OUTER JOIN dbo.Desk d ON m.DeskId = d.Id
WHERE IncludesCash = 1
AND [FactSet Fund Code] IS NOT NULL
AND [Fund Manager] IS NOT NULL
AND [FactSet Fund Code] <> 'TSNA_US'
AND [Asset Class] IS NOT NULL
AND FactsetDate IN ('"d"', '"d2"')
ORDER BY [Predicted Tracking Error] DESC"))
}
答案 0 :(得分:0)
“d”和“d2”'插入可能都失败了。
尝试在sqlQuery()
命令之外构建SQL查询字符串。
试试这个:
d <- "20180531"
d2 <- "20180228"
my_query <- paste0("SELECT fc.[FactSet Fund Code], fc.FactsetDate, fc.[Asset Class], fc.[Fund Manager], d.Id AS DeskID, d.[Name] AS DeskName, c.ClosingDate, m.NumericGrossPerformanceTarget, fc.[% Factor Risk], fc.[Predicted Tracking Error], fc.[Portfolio Predicted Beta], fc.[Port. Ending Active Share]
FROM [PortfolioAnalytics].[Data_Factset].[FundChar] fc
LEFT OUTER JOIN Data_Axioma.FactSetAccount fsa ON fc.FactsetAccountCode = REPLACE(fsa.Code, '_', '')
LEFT OUTER JOIN dbo.Portfolio p ON fsa.PortfolioId = p.Id
LEFT OUTER JOIN dbo.Mandate m ON p.UnderlyingId = m.id
INNER JOIN dbo.Mandate c ON p.UnderlyingId = c.id
LEFT OUTER JOIN dbo.Desk d ON m.DeskId = d.Id
WHERE IncludesCash = 1
AND [FactSet Fund Code] IS NOT NULL
AND [Fund Manager] IS NOT NULL
AND [FactSet Fund Code] <> 'TSNA_US'
AND [Asset Class] IS NOT NULL
AND FactsetDate IN ('", d, "', '", d2, "')
ORDER BY [Predicted Tracking Error] DESC")
myConn <- odbcDriverConnect(connection = "driver={SQL Server};server=SLSQL398_AGLIS\\SLSQL398;database=PortfolioAnalytics;trusted_connection=true")
data <- sqlFetch(myConn, "Data_Factset.FundChar", colnames = TRUE)
SQLCommand <- data.frame(sqlQuery(myConn, my_query))