采取以下示例代码:
with test as (
select to_date('01/2012', 'mm/yyyy') as dt, '1' as value from dual union all
select to_date('02/2012', 'mm/yyyy') as dt, '10' as value from dual union all
select to_date('03/2012', 'mm/yyyy') as dt, '100' as value from dual union all
select to_date('04/2012', 'mm/yyyy') as dt, '2' as value from dual union all
select to_date('05/2012', 'mm/yyyy') as dt, '20' as value from dual
)
select dt, value from test
返回:
DT | VALUE
1/1/2012 | 1
2/1/2012 | 10
3/1/2012 | 100
4/1/2012 | 2
5/1/2012 | 20
我希望我可以建立一个新列,其中包含每条记录的先前值,例如:
DT | VALUE | Previous
1/1/2012 | 1 | -
2/1/2012 | 10 | 1
3/1/2012 | 100 | 10
4/1/2012 | 2 | 100
5/1/2012 | 20 | 2
尽管我经常迷失在 connect_by 语句中,但这似乎很简单。
有人可以帮忙吗?