日期:2014-05-17 浏览次数:20966 次
WITH t AS( SELECT '2012-3-5' RT_T,115.18 RT_V,20 RT_H FROM dual UNION ALL SELECT '2012-3-5',115.18,20 FROM dual UNION ALL SELECT '2012-3-6',105.18,23 FROM dual UNION ALL SELECT '2012-7-4',106.00,09 FROM dual UNION ALL SELECT '2012-7-4',107.18,09 FROM dual UNION ALL SELECT '2012-7-4',110.18,15 FROM dual UNION ALL SELECT '2012-7-4',103.18,15 FROM dual UNION ALL SELECT '2012-7-4',108.18,15 FROM dual UNION ALL SELECT '2012-7-4',105.18,23 FROM dual UNION ALL SELECT '2012-7-5',105.18,11 FROM dual UNION ALL SELECT '2012-7-5',107.18,11 FROM dual ) SELECT rt_t,rt_h,Max(rt_v) FROM ( SELECT * FROM t WHERE To_Char(To_Date(rt_t||' '||RT_H),'yyyymmddhh24')>=To_Char(SYSDATE-1,'yyyymmddhh24') ) GROUP BY rt_t,rt_h ORDER BY rt_t,rt_h; 或者 SELECT DISTINCT rt_t,rt_h,Max(rt_v) over (PARTITION BY rt_t,rt_h ORDER BY rt_t,rt_h) AS MAX_V FROM ( SELECT * FROM t WHERE To_Char(To_Date(rt_t||' '||RT_H),'yyyymmddhh24')>=To_Char(SYSDATE-1,'yyyymmddhh24') )
------解决方案--------------------
WITH t AS( SELECT date'2012-3-5' RT_T,115.18 RT_V,20 RT_H,5 RT_D FROM dual UNION ALL SELECT date'2012-3-5',115.18,20,5 FROM dual UNION ALL SELECT date'2012-3-6',105.18,23,6 FROM dual UNION ALL SELECT date'2012-7-4',106.00,09,4 FROM dual UNION ALL SELECT date'2012-7-4',107.18,09,4 FROM dual UNION ALL SELECT date'2012-7-4',110.18,15,4 FROM dual UNION ALL SELECT date'2012-7-4',103.18,15,4 FROM dual UNION ALL SELECT date'2012-7-4',108.18,15,4 FROM dual UNION ALL SELECT date'2012-7-4',105.18,23,4 FROM dual UNION ALL SELECT date'2012-7-5',105.18,11,5 FROM dual UNION ALL SELECT date'2012-7-5',107.18,11,5 FROM dual ) select rt_t,rt_d, rt_h, max(rt_v) from t where to_date('2012-7-5 11', 'yyyy-mm-dd hh') - to_date(to_char(rt_t, 'yyyy-mm-dd') || ' ' || rt_h,'yyyy-mm-dd hh24') <= 1 group by rt_t,rt_d, rt_h order by rt_d,rt_h;
------解决方案--------------------