Sql 在列之间切换数据
以下面的示例代码为例:Sql 在列之间切换数据,sql,date,oracle11g,connect-by,Sql,Date,Oracle11g,Connect By,以下面的示例代码为例: 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 v
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语句感到迷茫
有人能帮忙吗?无需使用
连接方式。您只需要一个滞后
SQL> ed
Wrote file afiedt.buf
1 with test as (
2 select to_date('01/2012', 'mm/yyyy') as dt, '1' as value from dual union all
3 select to_date('02/2012', 'mm/yyyy') as dt, '10' as value from dual union all
4 select to_date('03/2012', 'mm/yyyy') as dt, '100' as value from dual union all
5 select to_date('04/2012', 'mm/yyyy') as dt, '2' as value from dual union all
6 select to_date('05/2012', 'mm/yyyy') as dt, '20' as value from dual
7 )
8 select dt,
9 value,
10 lag(value) over (order by dt) prior_value
11* from test
SQL> /
DT VAL PRI
--------- --- ---
01-JAN-12 1
01-FEB-12 10 1
01-MAR-12 100 10
01-APR-12 2 100
01-MAY-12 20 2
无需使用连接方式
。您只需要一个滞后
SQL> ed
Wrote file afiedt.buf
1 with test as (
2 select to_date('01/2012', 'mm/yyyy') as dt, '1' as value from dual union all
3 select to_date('02/2012', 'mm/yyyy') as dt, '10' as value from dual union all
4 select to_date('03/2012', 'mm/yyyy') as dt, '100' as value from dual union all
5 select to_date('04/2012', 'mm/yyyy') as dt, '2' as value from dual union all
6 select to_date('05/2012', 'mm/yyyy') as dt, '20' as value from dual
7 )
8 select dt,
9 value,
10 lag(value) over (order by dt) prior_value
11* from test
SQL> /
DT VAL PRI
--------- --- ---
01-JAN-12 1
01-FEB-12 10 1
01-MAR-12 100 10
01-APR-12 2 100
01-MAY-12 20 2