My SQL is a bit rusty and I\'m having quite a bit of difficulty with this problem. Suppose I have a table with a Timestamp column and a Number column. The goal is to return
Something like
with st
as (SELECT to_timestamp( '2012-06-06 12:40:00', 'yyyy-mm-dd hh24:mi:ss') +
numtodsinterval((level-1)*5, 'MINUTE') start_time,
to_timestamp( '2012-06-06 12:40:00', 'yyyy-mm-dd hh24:mi:ss') +
numtodsinterval(level*5, 'MINUTE') end_time
from dual
connect by level <= 10)
SELECT st.start_time, avg( yt.value )
FROM your_table yt,
st
WHERE yt.time between st.start_time and st.end_time
should work. Rather than generating 10 intervals and hard-coding the lowest interval, you can enhance the query to derive the starting point and the number of rows from the MIN(time) and MAX(time) in the table.