oracle 10g函式大全--分析函式

水之原發表於2013-08-27
oracle分析函式--SQL*PLUS環境
一、總體介紹
12.1 分析函式如何工作
語法 FUNCTION_NAME(<引數>,…) OVER (<PARTITION BY 表示式,…> <ORDER BY 表示式 <ASC DESC> <NULLS FIRST NULLS LAST>> <WINDOWING子句>) PARTITION子句 ORDER BY子句 WINDOWING子句 預設時相當於RANGE UNBOUNDED PRECEDING 
1. 值域窗(RANGE WINDOW) 
RANGE N PRECEDING 僅對數值或日期型別有效,選定窗為排序後當前行之前,某列(即排序列)值大於/小於(當前行該列值 –/+ N)的所有行,因此與ORDER BY子句有關係。 
2. 行窗(ROW WINDOW) 
ROWS N PRECEDING 選定窗為當前行及之前N行。 
還可以加上BETWEEN AND 形式,例如RANGE BETWEEN m PRECEDING AND n FOLLOWING 
函式 AVG(<distinct all> eXPr) 
一組或選定窗中表示式的平均值 CORR(expr, expr) 即COVAR_POP(exp1,exp2) / (STDDEV_POP(expr1) * STDDEV_POP(expr2)),兩個表示式的互相關,-1(反相關) ~ 1(正相關),0表示不相關 
COUNT(<distinct> <*> <expr>) 計數 
COVAR_POP(expr, expr) 總體協方差 
COVAR_SAMP(expr, expr) 樣本協方差 
CUME_DIST 累積分佈,即行在組中的相對位置,返回0 ~ 1 
DENSE_RANK 行的相對排序(與ORDER BY搭配),相同的值具有一樣的序數(NULL計為相同),並不留空序數 
FIRST_VALUE 一個組的第一個值 
LAG(expr, <offset>, <default>) 訪問之前的行,OFFSET是預設為1 的正數,表示相對行數,DEFAULT是當超出選定窗範圍時的返回值(如第一行不存在之前行) 
LAST_VALUE 一個組的最後一個值 
LEAD(expr, <offset>, <default>) 訪問之後的行,OFFSET是預設為1 的正數,表示相對行數,DEFAULT是當超出選定窗範圍時的返回值(如最後行不存在之前行) 
MAX(expr) 最大值 
MIN(expr) 最小值 
NTILE(expr) 按表示式的值和行在組中的位置編號,如表示式為4,則組分4份,分別為1 ~ 4的值,而不能等分則多出的部分在值最小的那組 
PERCENT_RANK 類似CUME_DIST,1/(行的序數 - 1) 
RANK 相對序數,答應並列,並空出隨後序號 
RATIO_TO_REPORT(expr) 表示式值 / SUM(表示式值) 
ROW_NUMBER 排序的組中行的偏移 
STDDEV(expr) 標準差 
STDDEV_POP(expr) 總體標準差 
STDDEV_SAMP(expr) 樣本標準差 
SUM(expr) 合計 
VAR_POP(expr) 總體方差 
VAR_SAMP(expr) 樣本方差 
VARIANCE(expr) 方差 
REGR_ xxxx(expr, expr) 線性迴歸函式 
REGR_SLOPE:返回斜率,等於COVAR_POP(expr1, expr2) / VAR_POP(expr2)
REGR_INTERCEPT:返回迴歸線的y截距,等於
AVG(expr1) - REGR_SLOPE(expr1, expr2) * AVG(expr2)
REGR_COUNT:返回用於填充迴歸線的非空數字對的數目
REGR_R2:返回迴歸線的決定係數,計算式為:
If VAR_POP(expr2) = 0 then return NULL
If VAR_POP(expr1) = 0 and VAR_POP(expr2) != 0 then return 1
If VAR_POP(expr1) > 0 and VAR_POP(expr2 != 0 then 
return POWER(CORR(expr1,expr),2)
REGR_AVGX:計算迴歸線的自變數(expr2)的平均值,去掉了空對(expr1, expr2)後,等於AVG(expr2)
REGR_AVGY:計算迴歸線的應變數(expr1)的平均值,去掉了空對(expr1, expr2)後,等於AVG(expr1)
REGR_SXX: 返回值等於REGR_COUNT(expr1, expr2) * VAR_POP(expr2)
REGR_SYY: 返回值等於REGR_COUNT(expr1, expr2) * VAR_POP(expr1)
REGR_SXY: 返回值等於REGR_COUNT(expr1, expr2) * COVAR_POP(expr1, expr2)


首先:建立表及接入測試資料
create table students
(id number(15,0),
area varchar2(10),
stu_type varchar2(2),
score number(20,2));
insert into students values(1, '111', 'g', 80 );
insert into students values(1, '111', 'j', 80 );
insert into students values(1, '222', 'g', 89 );
insert into students values(1, '222', 'g', 68 );
insert into students values(2, '111', 'g', 80 );
insert into students values(2, '111', 'j', 70 );
insert into students values(2, '222', 'g', 60 );
insert into students values(2, '222', 'j', 65 );
insert into students values(3, '111', 'g', 75 );
insert into students values(3, '111', 'j', 58 );
insert into students values(3, '222', 'g', 58 );
insert into students values(3, '222', 'j', 90 );
insert into students values(4, '111', 'g', 89 );
insert into students values(4, '111', 'j', 90 );
insert into students values(4, '222', 'g', 90 );
insert into students values(4, '222', 'j', 89 );
commit;

二、具體應用:
1、分組求和:
1)GROUP BY子句 
--A、GROUPING SETS

select id,area,stu_type,sum(score) score 
from students
group by grouping sets((id,area,stu_type),(id,area),id)
order by id,area,stu_type;

/*--------理解grouping sets
select a, b, c, sum( d ) from t
group by grouping sets ( a, b, c )

等效於

select * from (
select a, null, null, sum( d ) from t group by a
union all
select null, b, null, sum( d ) from t group by b 
union all
select null, null, c, sum( d ) from t group by c 
)
*/

--B、ROLLUP

select id,area,stu_type,sum(score) score 
from students
group by rollup(id,area,stu_type)
order by id,area,stu_type;

/*--------理解rollup
select a, b, c, sum( d )
from t
group by rollup(a, b, c);

等效於

select * from (
select a, b, c, sum( d ) from t group by a, b, c 
union all
select a, b, null, sum( d ) from t group by a, b
union all
select a, null, null, sum( d ) from t group by a
union all
select null, null, null, sum( d ) from t
)
*/

--C、CUBE

select id,area,stu_type,sum(score) score 
from students
group by cube(id,area,stu_type)
order by id,area,stu_type;

/*--------理解cube
select a, b, c, sum( d ) from t
group by cube( a, b, c)

等效於

select a, b, c, sum( d ) from t
group by grouping sets( 
( a, b, c ), 
( a, b ), ( a ), ( b, c ), 
( b ), ( a, c ), ( c ), 
() )
*/

--D、GROUPING
/*從上面的結果中我們很容易發現,每個統計資料所對應的行都會出現null,
如何來區分到底是根據那個欄位做的彙總呢,grouping函式判斷是否合計列!*/

select decode(grouping(id),1,'all id',id) id,
decode(grouping(area),1,'all area',to_char(area)) area,
decode(grouping(stu_type),1,'all_stu_type',stu_type) stu_type,
sum(score) score
from students
group by cube(id,area,stu_type)
order by id,area,stu_type; 

二、OVER()函式的使用
1、統計名次——DENSE_RANK(),ROW_NUMBER()
1)允許並列名次、名次不間斷,DENSE_RANK(),結果如122344456……
將score按ID分組排名:dense_rank() over(partition by id order by score desc)
將score不分組排名:dense_rank() over(order by score desc)
select id,area,score,
dense_rank() over(partition by id order by score desc) 分組id排序,
dense_rank() over(order by score desc) 不分組排序
from students order by id,area;

2)不允許並列名次、相同值名次不重複,ROW_NUMBER(),結果如123456……
將score按ID分組排名:row_number() over(partition by id order by score desc)
將score不分組排名:row_number() over(order by score desc)
select id,area,score,
row_number() over(partition by id order by score desc) 分組id排序,
row_number() over(order by score desc) 不分組排序
from students order by id,area;

3)允許並列名次、複製名次自動空缺,rank(),結果如12245558……
將score按ID分組排名:rank() over(partition by id order by score desc)
將score不分組排名:rank() over(order by score desc)
select id,area,score,
rank() over(partition by id order by score desc) 分組id排序,
rank() over(order by score desc) 不分組排序
from students order by id,area;

4)名次分析,cume_dist()——-最大排名/總個數 
函式:cume_dist() over(order by id)
select id,area,score,
cume_dist() over(order by id) a, --按ID最大排名/總個數 
cume_dist() over(partition by id order by score desc) b, --ID分組中,scroe最大排名值/本組總個數
row_number() over (order by id) 記錄號
from students order by id,area;


5)利用cume_dist(),允許並列名次、複製名次自動空缺,取並列後較大名次,結果如22355778……
將score按ID分組排名:cume_dist() over(partition by id order by score desc)*sum(1) over(partition by id)
將score不分組排名:cume_dist() over(order by score desc)*sum(1) over()
select id,area,score,
sum(1) over() as 總數,
sum(1) over(partition by id) as 分組個數,
(cume_dist() over(partition by id order by score desc))*(sum(1) over(partition by id)) 分組id排序,
(cume_dist() over(order by score desc))*(sum(1) over()) 不分組排序
from students order by id,area

2、分組統計--sum(),max(),avg(),RATIO_TO_REPORT()
select id,area,
sum(1) over() as 總記錄數, 
sum(1) over(partition by id) as 分組記錄數,
sum(score) over() as 總計 , 
sum(score) over(partition by id) as 分組求和,
sum(score) over(order by id) as  分組連續求和,
sum(score) over(partition by id,area) as 分組ID和area求和,
sum(score) over(partition by id order by area) as 分組ID並連續按area求和,
max(score) over() as 最大值,
max(score) over(partition by id) as 分組最大值,
max(score) over(order by id) as 分組連續最大值,
max(score) over(partition by id,area) as 分組ID和area求最大值,
max(score) over(partition by id order by area) as 分組ID並連續按area求最大值,
avg(score) over() as 所有平均,
avg(score) over(partition by id) as 分組平均,
avg(score) over(order by id) as 分組連續平均,
avg(score) over(partition by id,area) as 分組ID和area平均,
avg(score) over(partition by id order by area) as 分組ID並連續按area平均,
RATIO_TO_REPORT(score) over() as "佔所有%",
RATIO_TO_REPORT(score) over(partition by id) as "佔分組%",
score from students;

3、LAG(COL,n,default)、LEAD(OL,n,default) --取前後邊N條資料
取前面記錄的值:lag(score,n,x) over(order by id)
取後面記錄的值:lead(score,n,x) over(order by id) 
引數:n表示移動N條記錄,X表示不存在時填充值,iD表示排序列
select id,lag(score,1,0) over(order by id) lg,score from students;
select id,lead(score,1,0) over(order by id) lg,score from students;

4、FIRST_VALUE()、LAST_VALUE()
取第起始1行值:first_value(score,n) over(order by id)
取第最後1行值:LAST_value(score,n) over(order by id)
select id,first_value(score) over(order by id) fv,score from students;
select id,last_value(score) over(order by id) fv,score from students;

sum(...) over ...
【功能】連續求和分析函式
【引數】具體參示例
【說明】Oracle分析函式

NC示例:
select bdcode,sum(1) over(order by bdcode) aa from bd_bdinfo 

【示例】
1.原表資訊: SQL> break on deptno skip 1  -- 為效果更明顯,把不同部門的資料隔段顯示。
SQL> select deptno,ename,sal
   2   from emp
   3   order by deptno;

DEPTNO ENAME          SAL
---------- ---------- ----------
       10 CLARK          2450
          KING          5000
          MILLER           1300

       20 SMITH          800
          ADAMS          1100
          FORD          3000
          SCOTT          3000
          JONES          2975

       30 ALLEN          1600
          BLAKE          2850
          MARTIN           1250
          JAMES          950
          TURNER           1500
          WARD          1250

2.先來一個簡單的,注意over(...)條件的不同,
使用 sum(sal) over (order by ename)... 查詢員工的薪水“連續”求和,
注意over (order   by ename)如果沒有order by 子句,求和就不是“連續”的,
放在一起,體會一下不同之處:

SQL> select deptno,ename,sal,
   2   sum(sal) over (order by ename) 連續求和,
   3   sum(sal) over () 總和,                -- 此處sum(sal) over () 等同於sum(sal)
   4   100*round(sal/sum(sal) over (),4) "份額(%)"
   5   from emp
   6   /

DEPTNO ENAME          SAL 連續求和    總和 份額(%)
---------- ---------- ---------- ---------- ---------- ----------
       20 ADAMS          1100    1100    29025    3.79
       30 ALLEN          1600    2700    29025    5.51
       30 BLAKE          2850    5550    29025    9.82
       10 CLARK          2450    8000    29025    8.44
       20 FORD          3000    11000    29025    10.34
       30 JAMES          950    11950    29025    3.27
       20 JONES          2975    14925    29025    10.25
       10 KING          5000    19925    29025    17.23
       30 MARTIN           1250    21175    29025    4.31
       10 MILLER           1300    22475    29025    4.48
       20 SCOTT          3000    25475    29025    10.34
       20 SMITH          800    26275    29025    2.76
       30 TURNER           1500    27775    29025    5.17
       30 WARD          1250    29025    29025    4.31

3.使用子分割槽查出各部門薪水連續的總和。注意按部門分割槽。注意over(...)條件的不同,
sum(sal) over (partition by deptno order by ename) 按部門“連續”求總和
sum(sal) over (partition by deptno) 按部門求總和
sum(sal) over (order by deptno,ename) 不按部門“連續”求總和
sum(sal) over () 不按部門,求所有員工總和,效果等同於sum(sal)。

SQL> select deptno,ename,sal,
   2   sum(sal) over (partition by deptno order by ename) 部門連續求和,--各部門的薪水"連續"求和
   3   sum(sal) over (partition by deptno) 部門總和,   -- 部門統計的總和,同一部門總和不變
   4   100*round(sal/sum(sal) over (partition by deptno),4) "部門份額(%)",
   5   sum(sal) over (order by deptno,ename) 連續求和, --所有部門的薪水"連續"求和
   6   sum(sal) over () 總和,   -- 此處sum(sal) over () 等同於sum(sal),所有員工的薪水總和
   7   100*round(sal/sum(sal) over (),4) "總份額(%)"
   8   from emp
   9   /

DEPTNO ENAME SAL 部門連續求和 部門總和 部門份額(%) 連續求和 總和   總份額(%)
------ ------ ----- ------------ ---------- ----------- ---------- ------ ----------
10 CLARK 2450       2450    8750       28    2450   29025    8.44
   KING 5000       7450    8750    57.14    7450   29025    17.23
   MILLER   1300       8750    8750    14.86    8750   29025    4.48

20 ADAMS 1100       1100    10875    10.11    9850   29025    3.79
   FORD 3000       4100    10875    27.59    12850   29025    10.34
   JONES 2975       7075    10875    27.36    15825   29025    10.25
   SCOTT 3000        10075    10875    27.59    18825   29025    10.34
   SMITH 800        10875    10875        7.36    19625   29025    2.76

30 ALLEN 1600       1600    9400    17.02    21225   29025    5.51
   BLAKE 2850       4450    9400    30.32    24075   29025    9.82
   JAMES 950       5400    9400    10.11    25025   29025    3.27
   MARTIN   1250       6650    9400        13.3    26275   29025    4.31
   TURNER   1500       8150    9400    15.96    27775   29025    5.17
   WARD 1250       9400    9400        13.3    29025   29025    4.31


4.來一個綜合的例子,求和規則有按部門分割槽的,有不分割槽的例子
SQL> select deptno,ename,sal,sum(sal) over (partition by deptno order by sal) dept_sum,
   2   sum(sal) over (order by deptno,sal) sum
   3   from emp;

DEPTNO ENAME          SAL DEPT_SUM        SUM
---------- ---------- ---------- ---------- ----------
       10 MILLER           1300    1300    1300
          CLARK          2450    3750    3750
          KING          5000    8750    8750

       20 SMITH          800        800    9550
          ADAMS          1100    1900    10650
          JONES          2975    4875    13625
          SCOTT          3000    10875    19625
          FORD          3000    10875    19625

       30 JAMES          950        950    20575
          WARD          1250    3450    23075
          MARTIN           1250    3450    23075
          TURNER           1500    4950    24575
          ALLEN          1600    6550    26175
          BLAKE          2850    9400    29025

5.來一個逆序的,即部門從大到小排列,部門裡各員工的薪水從高到低排列,累計和的規則不變。

SQL> select deptno,ename,sal,
   2   sum(sal) over (partition by deptno order by deptno desc,sal desc) dept_sum,
   3   sum(sal) over (order by deptno desc,sal desc) sum
   4   from emp;

DEPTNO ENAME          SAL DEPT_SUM        SUM
---------- ---------- ---------- ---------- ----------
       30 BLAKE          2850    2850    2850
          ALLEN          1600    4450    4450
          TURNER           1500    5950    5950
          WARD          1250    8450    8450
          MARTIN           1250    8450    8450
          JAMES          950    9400    9400

       20 SCOTT          3000    6000    15400
          FORD          3000    6000    15400
          JONES          2975    8975    18375
          ADAMS          1100    10075    19475
          SMITH          800    10875    20275

       10 KING          5000    5000    25275
          CLARK          2450    7450    27725
          MILLER           1300    8750    29025


6.體會:在"... from emp;"後面不要加order   by 子句,使用的分析函式的(partition by deptno order by sal)
裡已經有排序的語句了,如果再在句尾新增排序子句,一致倒罷了,不一致,結果就令人費勁了。如:

SQL> select deptno,ename,sal,sum(sal) over (partition by deptno order by sal) dept_sum,
   2   sum(sal) over (order by deptno,sal) sum
   3   from emp
   4   order by deptno desc;

DEPTNO ENAME          SAL DEPT_SUM        SUM
---------- ---------- ---------- ---------- ----------
       30 JAMES          950        950    20575
          WARD          1250    3450    23075
          MARTIN           1250    3450    23075
          TURNER           1500    4950    24575
          ALLEN          1600    6550    26175
          BLAKE          2850    9400    29025

       20 SMITH          800        800    9550
          ADAMS          1100    1900    10650
          JONES          2975    4875    13625
          SCOTT          3000    10875    19625
          FORD          3000    10875    19625

       10 MILLER           1300    1300    1300
          CLARK          2450    3750    3750
          KING          5000    8750    8750


RANK()
dense_rank()
【語法】RANK ( ) OVER ( [query_partition_clause] order_by_clause )
	dense_RANK ( ) OVER ( [query_partition_clause] order_by_clause )

【功能】聚合函式RANK 和 dense_rank 主要的功能是計算一組數值中的排序值。
【引數】dense_rank與rank()用法相當,
【區別】dence_rank在並列關係是,相關等級不會跳過。rank則跳過
rank()是跳躍排序,有兩個第二名時接下來就是第四名(同樣是在各個分組內) 
dense_rank()l是連續排序,有兩個第二名時仍然跟著第三名。
【說明】Oracle分析函式


【示例】
聚合函式RANK 和 dense_rank 主要的功能是計算一組數值中的排序值。
  
  在9i版本之前,只有分析功能(analytic ),即從一個查詢結果中計算每一行的排序值,是基於order_by_clause子句中的value_exprs指定欄位的。
  
  其語法為:
  
  RANK ( ) OVER ( [query_partition_clause] order_by_clause )
  
  在9i版本新增加了合計功能(aggregate),即對給定的引數值在設定的排序查詢中計算出其排序值。這些引數必須是常數或常值表示式,且必須和ORDER BY子句中的欄位個數、位置、型別完全一致。
  
  其語法為:
  
  RANK ( expr [, expr]... ) WITHIN GROUP
  ( ORDER BY
  expr [ DESC | ASC ] [NULLS { FIRST | LAST }]
  [, expr [ DESC | ASC ] [NULLS { FIRST | LAST }]]...
  )
  
  例子1:
  
  有表Table內容如下
  
  COL1 COL2
    1 1
    2 1
    3 2
    3 1
    4 1
    4 2
    5 2
    5 2
    6 2
  
  分析功能:列出Col2分組後根據Col1排序,並生成數字列。比較實用於在成績表中查出各科前幾名的資訊。
  
  SELECT a.*,RANK() OVER(PARTITION BY col2 ORDER BY col1) "Rank" FROM table a;
  
  結果如下:
  
  COL1 COL2 Rank
    1 1   1
    2 1   2
    3 1   3
    4 1   4
    3 2   1
    4 2   2
    5 2   3
    5 2   3
    6 2   5
  
  例子2:
  
  TABLE:A (科目,分數)
  
  數學,80
  語文,70
  數學,90
  數學,60
  數學,100
  語文,88
  語文,65
  語文,77
  
  現在我想要的結果是:(即想要每門科目的前3名的分數)
    數學,100
  數學,90
  數學,80
  語文,88
  語文,77
  語文,70
  
  那麼語句就這麼寫:
  
  select * from (select rank() over(partition by 科目 order by 分數 desc) rk,a.* from a) t
  where t.rk<=3;
  
  例子3:
  
  合計功能:計算出數值(4,1)在Orade By Col1,Col2排序下的排序值,也就是col1=4,col2=1在排序以後的位置
  
  SELECT RANK(4,3) WITHIN GROUP (ORDER BY col1,col2) "Rank" FROM table;
  
  結果如下:
  Rank
  4
  
  dense_rank與rank()用法相當,但是有一個區別:dence_rank在並列關係是,相關等級不會跳過。rank則跳過
  
  例如:表
  
  A      B      C
  a     liu     wang
  a     jin     shu
  a     cai     kai
  b     yang     du
  b     lin     ying
  b     yao     cai
  b     yang     99
  
  例如:當rank時為:
  
  select m.a,m.b,m.c,rank() over(partition by a order by b) liu from test3 m
  
   A     B       C     LIU
   a     cai      kai     1
   a     jin      shu     2
   a     liu      wang     3
   b     lin      ying     1
   b     yang     du      2
   b     yang     99      2
   b     yao      cai     4
  
  而如果用dense_rank時為:
  
  select m.a,m.b,m.c,dense_rank() over(partition by a order by b) liu from test3 m
  
   A     B       C     LIU
   a     cai     kai     1
   a     jin     shu     2
   a     liu     wang     3
   b     lin     ying     1
   b     yang     du      2
   b     yang     99      2
   b     yao     cai     3 

ROW_NUMBER()
【語法】ROW_NUMBER() OVER (PARTITION BY COL1 ORDER BY COL2) 
【功能】表示根據COL1分組,在分組內部根據 COL2排序,而這個值就表示每組內部排序後的順序編號(組內連續的唯一的) 
row_number() 返回的主要是“行”的資訊,並沒有排名
【引數】
【說明】Oracle分析函式

主要功能:用於取前幾名,或者最後幾名等

【示例】
表內容如下:
name | seqno | description
A | 1 | test
A | 2 | test
A | 3 | test
A | 4 | test
B | 1 | test
B | 2 | test
B | 3 | test
B | 4 | test
C | 1 | test
C | 2 | test
C | 3 | test
C | 4 | test

我想有一個sql語句,搜尋的結果是 
A | 1 | test
A | 2 | test
B | 1 | test
B | 2 | test
C | 1 | test
C | 2 | test
實現: 
select name,seqno,description 
from(select name,seqno,description,row_number() over (partition by name order by seqno) id
from table_name) where id<=3;
lag()和lead()
【語法】
lag(EXPR,<OFFSET>,<DEFAULT>)
LEAD(EXPR,<OFFSET>,<DEFAULT>)
【功能】表示根據COL1分組,在分組內部根據 COL2排序,而這個值就表示每組內部排序後的順序編號(組內連續的唯一的) 
lead () 下一個值 lag() 上一個值

【引數】
EXPR是從其他行返回的表示式 
OFFSET是預設為1 的正數,表示相對行數。希望檢索的當前行分割槽的偏移量
DEFAULT是在OFFSET表示的數目超出了分組的範圍時返回的值。
【說明】Oracle分析函式

【示例】
-- Create table
create table LEAD_TABLE
(
 CASEID VARCHAR2(10),
 STEPID VARCHAR2(10),
 ACTIONDATE DATE
)
tablespace COLM_DATA
 pctfree 10
 initrans 1
 maxtrans 255
 storage
 (
 initial 64K
 minextents 1
 maxextents unlimited
 );

insert into LEAD_TABLE values('Case1','Step1',to_date('20070101','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case1','Step2',to_date('20070102','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case1','Step3',to_date('20070103','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case1','Step4',to_date('20070104','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case1','Step5',to_date('20070105','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case1','Step4',to_date('20070106','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case1','Step6',to_date('20070101','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case1','Step1',to_date('20070201','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case2','Step2',to_date('20070202','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case2','Step3',to_date('20070203','yyyy-mm-dd'));
commit;
 
結果如下:

Case1 Step1 2007-1-1 Step2 2007-1-2 
Case1 Step2 2007-1-2 Step3 2007-1-3 Step1 2007-1-1
Case1 Step3 2007-1-3 Step4 2007-1-4 Step2 2007-1-2
Case1 Step4 2007-1-4 Step5 2007-1-5 Step3 2007-1-3
Case1 Step5 2007-1-5 Step4 2007-1-6 Step4 2007-1-4
Case1 Step4 2007-1-6 Step6 2007-1-7 Step5 2007-1-5
Case1 Step6 2007-1-7 Step4 2007-1-6
Case2 Step1 2007-2-1 Step2 2007-2-2 
Case2 Step2 2007-2-2 Step3 2007-2-3 Step1 2007-2-1
Case2 Step3 2007-2-3 Step2 2007-2-2

還可以進一步統計一下兩者的相差天數

select caseid,stepid,actiondate,nextactiondate,nextactiondate-actiondate datebetween from (
select caseid,stepid,actiondate,lead(stepid) over (partition by caseid order by actiondate) nextstepid,
lead(actiondate) over (partition by caseid order by actiondate) nextactiondate,
lag(stepid) over (partition by caseid order by actiondate) prestepid,
lag(actiondate) over (partition by caseid order by actiondate) preactiondate
from lead_table) 
結果如下:

Case1 Step1 2007-1-1 2007-1-2 1
Case1 Step2 2007-1-2 2007-1-3 1
Case1 Step3 2007-1-3 2007-1-4 1
Case1 Step4 2007-1-4 2007-1-5 1
Case1 Step5 2007-1-5 2007-1-6 1
Case1 Step4 2007-1-6 2007-1-7 1
Case1 Step6 2007-1-7 
Case2 Step1 2007-2-1 2007-2-2 1
Case2 Step2 2007-2-2 2007-2-3 1
Case2 Step3 2007-2-3 
 
每一條記錄都能連線到上/下一行的內容

lead () 下一個值 lag() 上一個值

select caseid,stepid,actiondate,lead(stepid) over (partition by caseid order by actiondate) nextstepid,
lead(actiondate) over (partition by caseid order by actiondate) nextactiondate,
lag(stepid) over (partition by caseid order by actiondate) prestepid,
lag(actiondate) over (partition by caseid order by actiondate) preactiondate
from lead_table

相關文章