注意:與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