SQL查询 - 平均数据增加

时间:2012-07-25 08:59:45

标签: sql vb.net sql-server-2008

我每x小时进行一次数据轮询,它基于液位。水平总是上升。

我需要知道如何计算一周(每天)的平均增长。

因此,如果天数据显示为1000-1200-1300-1400,那么我们知道平均每天增加100个。

DateReceived  DeviceLevel Serial

1/mm/yyyy     3200         346
1/mm/yyyy     12000        222
2/mm/yyyy     3560         346
2/mm/yyyy     13600         222

尝试使用

AVG()

功能,但没有失败。

2 个答案:

答案 0 :(得分:1)

您可以使用自我加入来查找前一天的级别。这会让你增加一天的时间。外部查询可用于查找一周内的平均增长:

select  datepart(wk, Day) as WeekNr
,       avg(IncreasePerDay) as AverageDailyIncrease
from    (
        select  cast(d1.MeasureDt as date) as Day
        ,       avg(d2.level) - avg(d1.level) as IncreasePerDay
        from    YourTable d1
        join    YourTable d2
        on      cast(d2.MeasureDt as date) = cast(d1.MeasureDt as date) + 1
        group by
                cast(d1.MeasureDt as date)
        ) as SubQueryAlias
group by
        datepart(wk, Day)

答案 1 :(得分:0)

为你的桌子#t

select t2.serial, avg(t2.devicelevel-t1.devicelevel) from #t t1
    inner join #t t2 on t1.datereceived = dateadd(d,1,t2.datereceived)
group by t2.serial

间隔4小时......

create table #t (polldate datetime, amount int)
insert #t values ('2012-06-01 16:00', 200),
('2012-06-01 20:00', 220),
('2012-06-02 00:00', 250),
('2012-06-02 04:00', 270)

select avg(t1.amount-t2.amount) from #t t1
    inner join #t t2 on t1.polldate = dateadd(hh,4,t2.polldate)

drop table #t