日期:2014-05-17 浏览次数:21268 次
with t1 as
(
select '2011-01' dt,85 sc from dual union all
select '2011-02' dt,98 sc from dual union all
select '2011-03' dt,56 sc from dual union all
select '2011-04' dt,66 sc from dual union all
select '2012-01' dt,33 sc from dual union all
select '2012-02' dt,34 sc from dual union all
select '2012-03' dt,54 sc from dual union all
select '2012-04' dt,69 sc from dual union all
select '2013-01' dt,97 sc from dual union all
select '2013-02' dt,43 sc from dual union all
select '2013-03' dt,44 sc from dual union all
select '2013-04' dt,34 sc from dual
)
select dt,sc,s_sc,
(select max(sc) from t1 where t1.dt <= t.dt and substr(t1.dt,-2)=substr(t.dt,-2)) m_sc
from
(
select dt,sc,
case row_number() over(partition by substr(dt,-2) order by rownum)
when 1 then null
else lag(sc) over(order by substr(dt,-2),substr(dt,1,4)) end s_sc
from t1
) t
order by dt
dt sc s_sc