3,499
社区成员
发帖
与我相关
我的任务
分享
with tbl as
(
select '201101' as sdate, '001' as id, 11 as price from dual
union all
select '201101' as sdate, '002' as id, 12 as price from dual
union all
select '201102' as sdate, '001' as id, 21 as price from dual
union all
select '201102' as sdate, '002' as id, 22 as price from dual
)
select sdate, id, price, nvl(lag(price) over(partition by id order by sdate), 0) as last_price
from tbl
order by sdate, id;
SDATE ID PRICE LAST_PRICE
------ --- ---------- ----------
201101 001 11 0
201101 002 12 0
201102 001 21 11
201102 002 22 12