5
按小时按大型数据集分组
我使用MS SQL 2008从250万条记录中选择一个平均字段。每条记录代表一秒钟。MyField是这些1秒记录的每小时平均值。当然,服务器CPU达到100%,选择时间太长。我可能需要保存这些平均值,以便SQL不必在每个请求中都选择所有这些记录。该怎么办? SELECT DISTINCT CONVERT(VARCHAR, [timestamp], 1)+' '+ CAST(DATEPART(Hh,[timestamp]) as VARCHAR) AS TimeStampHour, MIN([timestamp]) as TimeStamp, AVG(MyField) As AvgField FROM MyData WHERE TimeStamp > '4/10/2011' GROUP BY CONVERT(VARCHAR, [timestamp], 1)+' '+ CAST(DATEPART(Hh,[timestamp]) as VARCHAR) ORDER BY TimeStamp
12
sql-server
query