天天看點

LEAD分析函數用法小例子

--可以用ORACLE資料庫提供的LEAD函數構造連續區間

--原值

SQL> select * from test1;

    ACCTNO CURRENT_BAL DXN_DT

---------- ----------- --------

       201         800 20120101

       201         520 20120809

       202         100 20120106

       202         200 20120808

--處理後結果

--方法一

SQL> select t.acctno,

  2         t.dxn_dt,

  3         lead(t.dxn_dt,1,to_date('30001231', 'yyyymmdd')) over(partition by t.acctno order by t.dxn_dt),

  4         t.current_bal

  5    from test1 t;

    ACCTNO DXN_DT   LEAD(T.D CURRENT_BAL

---------- -------- -------- -----------

       201 20120101 20120809         800

       201 20120809 30001231         520

       202 20120106 20120808         100

       202 20120808 30001231         200

--方法二

select t.acctno,

       t.dxn_dt,

       nvl(lead(t.dxn_dt) over(partition by t.acctno order by t.dxn_dt),to_date('30001231', 'yyyymmdd')),

       t.current_bal

  from test1 t;