0

我每 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()

功能但无济于事。

4

2 回答 2

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)
于 2012-07-25T09:06:40.687 回答
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
于 2012-07-25T09:06:21.593 回答