Aggregate function over a given time interval

前端 未结 4 1186
灰色年华
灰色年华 2020-12-30 12:11

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

4条回答
  •  梦毁少年i
    2020-12-30 12:35

    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.

提交回复
热议问题