【Analytic】分析函式之FIRST_VALUE函式

secooler發表於2010-01-15
我們透過這個文章來看一下分析函式FIRST_VALUE的用法。
【Analytic】分析函式之MIN函式:http://space.itpub.net/519536/viewspace-624736
【Analytic】分析函式之MAX函式:http://space.itpub.net/519536/viewspace-624749
【Analytic】分析函式之AVG函式:http://space.itpub.net/519536/viewspace-624799
【Analytic】分析函式之ROW_NUMBER函式:http://space.itpub.net/519536/viewspace-624886
【Analytic】分析函式之RANK函式:http://space.itpub.net/519536/viewspace-624985
【Analytic】分析函式之DENSE_RANK函式:http://space.itpub.net/519536/viewspace-625115
【Analytic】分析函式之COUNT函式:http://space.itpub.net/519536/viewspace-625191

1.萬變不離其宗,先看FIRST_VALUE分析函式的10g語法描述。

FIRST_VALUE (expr [ IGNORE NULLS ])
   OVER (analytic_clause)

2.透過實驗看一下分析函式FIRST_VALUE的使用方法
1)建立測試表T,並初始化9條資料。
sec@ora10g> create table t (group_id number(10), name varchar2(10), salary int);
sec@ora10g> insert into t values (1,'Tom',1200);
sec@ora10g> insert into t values (2,'Kary',2400);
sec@ora10g> insert into t values (2,'Joe',800);
sec@ora10g> insert into t values (3,'Andy',100);
sec@ora10g> insert into t values (3,'Ellen',200);
sec@ora10g> insert into t values (3,'Erick',300);
sec@ora10g> insert into t values (3,'Hou',400);
sec@ora10g> insert into t values (3,'Mary',500);
sec@ora10g> insert into t values (3,'Secooler',600);
sec@ora10g> commit;

2)T表全貌
sec@ora10g> select * from t order by group_id,name;

  GROUP_ID NAME                               SALARY
---------- ------------------------------ ----------
         1 Tom                                  1200
         2 Joe                                   800
         2 Kary                                 2400
         3 Andy                                  100
         3 Ellen                                 200
         3 Erick                                 300
         3 Hou                                   400
         3 Mary                                  500
         3 Secooler                              600

9 rows selected.

共三組資料,group_id分別是1、2和3。第1組有一個人,第2組有兩個人,第3組有六個人。最後一列是每個人的薪水值。

3)FIRST_VALUE分析函式的簡單用法
(1)在T表中新增一列,標識每一個資料分割槽中薪水最低的人名。
sec@ora10g> select group_id, name, salary, FIRST_VALUE(name) OVER (partition by group_id order by salary asc ) as lowest_sal_name from t order by group_id, name;

  GROUP_ID NAME                               SALARY LOWEST_SAL
---------- ------------------------------ ---------- ----------
         1 Tom                                  1200 Tom
         2 Joe                                   800 Joe
         2 Kary                                 2400 Joe
         3 Andy                                  100 Andy
         3 Ellen                                 200 Andy
         3 Erick                                 300 Andy
         3 Hou                                   400 Andy
         3 Mary                                  500 Andy
         3 Secooler                              600 Andy

9 rows selected.

(2)在T表中新增一列,標識每一個資料分割槽中薪水最低的薪水值。
sec@ora10g> select group_id, name, salary, FIRST_VALUE(SALARY) OVER (partition by group_id order by salary asc ) as lowest_sal_name from t order by group_id, name;

  GROUP_ID NAME                               SALARY LOWEST_SAL_NAME
---------- ------------------------------ ---------- ---------------
         1 Tom                                  1200            1200
         2 Joe                                   800             800
         2 Kary                                 2400             800
         3 Andy                                  100             100
         3 Ellen                                 200             100
         3 Erick                                 300             100
         3 Hou                                   400             100
         3 Mary                                  500             100
         3 Secooler                              600             100

9 rows selected.

3.小結
分析函式FIRST_VALUE可以非常便利並迅速的的得到排序後的第一條資料的各種資訊,善用之。
有關分析函式的擴充套件可以參考Oracle的官方文件中的“Analytic Functions”描述:

Good luck.

secooler
10.01.15

-- The End --

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-625280/,如需轉載,請註明出處,否則將追究法律責任。

相關文章