我正在使用RODBC
在R中进行SQL查询。
查询以格式
提供数据Date(POSIXct); var1:var29
%Y-%m-%d %H:%M:%S; numeric_values
问题是,一旦查询超过一定长度,6960左右有29-30个变量,包括日期,传递给R的信息开始如下:
Date(POSIXct); var1:var30
%Y-%m-%d; numeric_values
因此,我丢失了"%H:%M:%S"
信息。我不知道为什么。如果我减少变量的数量,我可以在此之前增加时间长度。
在Windows服务器2007上是ts-sql
。(我相信)
R中的SQL调用示例:
sqlQuery(database, "SELECT [datetime], [0] as SYS, [1] as NO1, [2] as NO2, [7] as NO3, [9] as NO4, [19] as NO5, [5] as DK1,[6] as DK2, [25] as SE1,
[26] as SE2,[27] as SE3, [28] as SE4, [4] as FIN, [13] as DE, [14] as NL, [16] as FR, [15] as CH, [17] as AT, [20] as EE,
[36] as LT, [45] as LV, [42] as SI, [50] as IT, [44] as ES, [43] as BE, [74] as HU, [75] as CZ, [41] as UK
From
(
SELECT [area_id],[pris],[datetime]
FROM [BigData].[dbo].[Prices]
WHERE area_id in (0,1,2,7,9,19,5,6,25,26,27,28,4,13,14,16,15,17, 20, 36, 45, 42, 50, 44, 43, 74, 75, 41)
AND [datetime]>= cast(GETDATE()-290 as date)
AND [datetime]< cast(GETDATE()+0 as date)
) p
PIVOT(SUM([pris])
FOR [area_id] IN
([0], [1], [2], [7], [9], [19], [5],[6], [25],[26],[27], [28], [4], [13], [14], [16], [15], [17], [20],
[36], [45], [42], [50], [44], [43], [74], [75], [41]))
AS pvt
ORDER BY [datetime] asc ") -> prices
答案 0 :(得分:1)
您可以使用answer from kristang :(使用as.is
选项调用sqlQuery,在字符串中获取时间戳,并在R中使用as.POSIXct
转换列。
但我认为更有效的解决方案是通过SQL表达式(SQL Server的示例)获取数值类型的日期时间值:
sqlQuery( "select convert(float, my_date)*3600*24 as my_date from ...")
并将其从数字转换为POSIXct
:
df1$my_date <- as.POSIXct(df1$my_date, origin = "1900-01-01", tz = "UTC")
由于POSIXct
本质上是数字,因此获取和类型转换比使用RODBC的普通sqlQuery运行得更快。 RODBC从文本字符串转换每个时间戳(在sqlGetResults函数中看起来为.POSIXct用法)。因此,即使RODBC返回完整的时间戳,解决方案也是合理的。
P.S。如果您真的想从文本转换,请参阅fasttime包中的fastPOSIXct。