Oracle 的分析函数你知道吗?

解决方案 »

  1.   

    SELECT *
      FROM (SELECT   t5.ID, t1.num + t2.num + t3.num + t4.num + t5.num ttl
                FROM mytable t1,
                     mytable t2,
                     mytable t3,
                     mytable t4,
                     mytable t5
               WHERE t1.ID = t2.ID + 1
                 AND t1.ID = t3.ID + 2
                 AND t1.ID = t4.ID + 3
                 AND t1.ID = t5.ID + 4
            ORDER BY ttl)
     WHERE ROWNUM = 1;
      

  2.   

    select * 
      from ( select a.time starttime, max(b.time) endtime, sum(b.num) total 
               from mytable a, mytable b
               where b.time>=a.time and b.time<a.time+1
               group by a.time
               order by 3 desc)
      where rownum=1;
      

  3.   

    select * from mytable a, (
    select id_start, id_end from 
    (select id,num, 
            sum(num) over (order by id rows between 4 preceding and current row) sm,
            first_value(id) over (order by id rows between 4 preceding and current row) id_start,
            last_value(id) over (order by id rows between 4 preceding and current row) id_end
    from t order by sm desc) where rownum=1) b
    where a.id between b.id_start and id_end
      

  4.   

    楼上的那句是连续5条记录和最大的查询,下面是连续24小时记录和最大:select * from mytable a, (
    select id_start, id_end from 
    (select id,num, 
            sum(num) over
            (order by time range numToDSInterval(24,'HOUR') preceding) sum_num,
            first_value(id) over
            (order by time range numToDSInterval(48,'HOUR') preceding) id_start,
            last_value(id) over
            (order by itme range numToDSInterval(48,'HOUR') preceding) id_end
    from t order by sum_num desc) where rownum=1) b
    where a.id between b.id_start and id_end