sql - Aggregate function over a given time interval -


my sql bit rusty , i'm having quite bit of difficulty problem. suppose have table timestamp column , number column. goal return result set containing average value arbitrarily chosen regular interval.

so, example, if had following initial data, resulting output 5 minute interval follows:

time                               value -------------------------------    ----- 06-jun-12 12.40.00.000000000 pm      2 06-jun-12 12.41.35.000000000 pm      3 06-jun-12 12.43.22.000000000 pm      4 06-jun-12 12.47.55.000000000 pm      5 06-jun-12 12.52.00.000000000 pm      2 06-jun-12 12.54.59.000000000 pm      3 06-jun-12 12.56.01.000000000 pm      4  output:  start_time                         avg_value -------------------------------    --------- 06-jun-12 12.40.00.000000000 pm      3 06-jun-12 12.45.00.000000000 pm      5 06-jun-12 12.50.00.000000000 pm      2.5 06-jun-12 12.55.00.000000000 pm      4 

note oracle database, oracle-specific solutions work fine. could, of course, done stored procedure hoping accomplish task in single query.

create table tt (time timestamp, value number);  insert tt (time, value) values ('06-jun-12 12.40.00.000000000 pm', 2); insert tt (time, value) values ('06-jun-12 12.41.35.000000000 pm', 3); insert tt (time, value) values ('06-jun-12 12.43.22.000000000 pm', 4); insert tt (time, value) values ('06-jun-12 12.47.55.000000000 pm', 5); insert tt (time, value) values ('06-jun-12 12.52.00.000000000 pm', 2); insert tt (time, value) values ('06-jun-12 12.54.59.000000000 pm', 3); insert tt (time, value) values ('06-jun-12 12.56.01.000000000 pm', 4);   tmin (     select min(time) t tt ),   tmax (     select max(time) t tt ) select ranges.inf, ranges.sup, avg(tt.value)      (         select              5*(level-1)*(1/24/60) + tmin.t inf,             5*(level)*(1/24/60) + tmin.t sup         tmin, tmax         connect (5*(level-1)*(1/24/60) + tmin.t) < tmax.t     ) ranges join tt on tt.time between ranges.inf , ranges.sup group ranges.inf, ranges.sup order ranges.inf 

fiddle: http://sqlfiddle.com/#!4/9e314/11

edit: beated justin, usual... :-)


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -