oracle分析函數系列之first_value/last_value:在記錄集中尋找第一條記錄和最後一條記錄____靜態函數

來源:互聯網
上載者:User

注意:與max,min的區別,雖然也可以實現,但只是針對數字欄位.

1、初始化未經處理資料:
create table test (id number(2), name varchar2(10), salary number(6,2));
insert into test values (1,'Tom',120);
insert into test values (2,'Ellen',240);
insert into test values (2,'Joe',80);
insert into test values (3,'Andy',300);
insert into test values (3,'Kary',500);
insert into test values (3,'Erick',1300);
insert into test values (3,'Hou',40);
insert into test values (3,'Mary',200);
insert into test values (3,'Secooler',800);
commit;
select * from test order by ID,name;

 ID NAME         SALARY
--- ---------- --------
  1 Tom          120.00
  2 Ellen        240.00
  2 Joe           80.00
  3 Andy         300.00
  3 Erick       1300.00
  3 Hou           40.00
  3 Kary         500.00
  3 Mary         200.00
  3 Secooler     800.00


2、LAST_VALUE分析函數的簡單用法
(1)在TEST表中添加一列,標識每一個資料分區中薪水最高的人名。
select ID, name, salary, LAST_VALUE(name) OVER (partition by ID order by salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as highest_sal_name from test order by ID, name;

 ID NAME         SALARY HIGHEST_SAL_NAME
--- ---------- -------- ----------------
  1 Tom          120.00 Tom
  2 Ellen        240.00 Ellen
  2 Joe           80.00 Ellen
  3 Andy         300.00 Erick
  3 Erick       1300.00 Erick
  3 Hou           40.00 Erick
  3 Kary         500.00 Erick
  3 Mary         200.00 Erick
  3 Secooler     800.00 Erick

注意其中“ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING”的使用,若省略效果如下。
select ID, name, salary, LAST_VALUE(name) OVER (partition by ID order by salary) as highest_sal_name from test order by ID,name;

 ID NAME         SALARY HIGHEST_SAL_NAME
--- ---------- -------- ----------------
  1 Tom          120.00 Tom
  2 Ellen        240.00 Ellen
  2 Joe           80.00 Joe
  3 Andy         300.00 Andy
  3 Erick       1300.00 Erick
  3 Hou           40.00 Hou
  3 Kary         500.00 Kary
  3 Mary         200.00 Mary
  3 Secooler     800.00 Secooler

顯然這不是我們想要的效果:(,這是為什麼呢~~~。給您一次思考和回答的機會。
如果對UNBOUNDED PRECEDING和UNBOUNDED FOLLOWING不熟悉,請參考Oracle官方文檔“windowing_clause”http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions001.htm#i97640。

在TEST表中添加一列,標識每一個資料分區中薪水最高的薪水值。
col highest_sal_name for 9999
select ID, name, salary, LAST_VALUE(SALARY) OVER (partition by ID order by salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as highest_sal_name from test order by ID, name;

 ID NAME         SALARY HIGHEST_SAL_NAME
--- ---------- -------- ----------------
  1 Tom          120.00              120
  2 Ellen        240.00              240
  2 Joe           80.00              240
  3 Andy         300.00             1300
  3 Erick       1300.00             1300
  3 Hou           40.00             1300
  3 Kary         500.00             1300
  3 Mary         200.00             1300
  3 Secooler     800.00             1300

3、與之相對應的是FIRST_VALUE函數
select ID, name, salary, FIRST_VALUE(name) OVER (partition by ID order by salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as highest_sal_name from test order by ID, name;
 ID NAME         SALARY HIGHEST_SAL_NAME
--- ---------- -------- ----------------
  1 Tom          120.00 Tom
  2 Ellen        240.00 Joe
  2 Joe           80.00 Joe
  3 Andy         300.00 Hou
  3 Erick       1300.00 Hou
  3 Hou           40.00 Hou
  3 Kary         500.00 Hou
  3 Mary         200.00 Hou
  3 Secooler     800.00 Hou

select ID, name, salary, FIRST_VALUE(SALARY) OVER (partition by ID order by salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as highest_sal_name from test order by ID, name;
 ID NAME         SALARY HIGHEST_SAL_NAME
--- ---------- -------- ----------------
  1 Tom          120.00              120
  2 Ellen        240.00               80
  2 Joe           80.00               80
  3 Andy         300.00               40
  3 Erick       1300.00               40
  3 Hou           40.00               40
  3 Kary         500.00               40
  3 Mary         200.00               40
  3 Secooler     800.00               40

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.