任意值:
Select RecID, [RamCode],EquipMentID,[RecPressure],[ColTime] FROM
(SELECT RecID,[RamCode],EquipMentID,[RecPressure],[ColTime],datediff(mi,[ColTime],DATEADD(mi,-5,CASE WHEN DATEPART(mi,GETDATE())%5=0 THEN DATEADD(mi,1,GETDATE()) ELSE GETDATE()END)) AS interval
FROM [KS_DataRecord] ) AS t WHERE t.interval%5 = 0
平均值:
-- This convert the period to date-time format
SELECT
-- note the 5, the "minute", and the starting point to convert the
-- period back to original time
DATEADD(minute, AP.FiveMinutesPeriod * 5, '2010-01-01T00:00:00') AS Period,
AP.AvgValue
FROM
-- this groups by the period and gets the average
(SELECT
P.FiveMinutesPeriod,
AVG(P.RecPressure) AS AvgValue
FROM
-- This calculates the period (five minutes in this instance)
(SELECT
-- note the division by 5 and the "minute" to build the 5 minute periods
-- the '2010-01-01T00:00:00' is the starting point for the periods
datediff(minute, '2010-01-01T00:00:00', T.ColTime)/5 AS FiveMinutesPeriod,
T.RecPressure
FROM [KS_DataBase].[dbo].[KS_DataRecord] T) AS P
GROUP BY P.FiveMinutesPeriod) AP order by Period