SQL 5分钟内任意值,平均值

本文介绍了一种使用SQL从数据库中选择特定时间间隔内的记录的方法,并提供了计算这些记录平均值的技术。通过对时间戳进行数学运算来精确地筛选和聚合数据,从而实现每五分钟周期的数据收集与分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

任意值:

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 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值