绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
【Analytic】分析函数之LAST_VALUE函数
2020-01-25 15:02:55
我们通过这个文章来看一下分析函数LAST_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
【Analytic】分析函数之FIRST_VALUE函数:http://space.itpub.net/519536/viewspace-625280

1.万变不离其宗,先看LAST_VALUE分析函数的10g语法描述。
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions073.htm#i83648
LAST_VALUE(expr [ IGNORE NULLS ])
   OVER (analytic_clause)

2.通过实验看一下分析函数LAST_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)LAST_VALUE分析函数的简单用法
(1)在T表中添加一列,标识每一个数据分区中薪水高的人名。
sec@ora10g> col highest_sal_name for a16
sec@ora10g> select group_id, name, salary, LAST_VALUE(name) OVER (partition by group_id order by salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as highest_sal_name from t order by group_id, name;

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

9 rows selected.


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

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

9 rows selected.


显然这不是我们想要的效果:(,这是为什么呢~~~?给您一次思考和回答的机会。
如果对UNBOUNDED PRECEDING和UNBOUNDED FOLLOWING不熟悉,请参考Oracle官方文档“windowing_clause”http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions001.htm#i97640。

(2)在T表中添加一列,标识每一个数据分区中薪水高的薪水值。
sec@ora10g> col highest_sal_name for 9999
sec@ora10g> select group_id, name, salary, LAST_VALUE(SALARY) OVER (partition by group_id order by salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as highest_sal_name from t order by group_id, name;

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

9 rows selected.

3.小结
分析函数LAST_VALUE可以非常便利并迅速的的得到排序后的后一条数据的各种信息。但,细节之处不容忽视,细心使用。
有关分析函数的扩展可以参考Oracle的官方文档中的“Analytic Functions”描述:http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions001.htm#SQLRF06174

Good luck.

secooler
10.01.16

-- The End --

分享好友

分享这个小栈给你的朋友们,一起进步吧。

OCM联盟
创建时间:2019-12-27 14:04:54
OCM联盟(OCMU – Oracle Certified Master Union)是一群有着共同理想,共同志向的DBA的家。 ⚠️该小栈仅限ocm成员入驻!审核制! Oracle Certified Master (OCM) -Oracle认证大师,是Oracle认证的别,是对数据库从业人员的技术、知识和操作技能的别的认可。Oracle OCM是解决困难的技术难题和复杂的系统故障的佳Oracle专家人选,也是IT行业衡量IT专家和经理人的高专业程度及经验的基准。
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

栈主、嘉宾

查看更多
  • 侯圣文@secooler
    栈主

小栈成员

查看更多
  • gaokeke123
  • ?
  • 山中老狐狸
  • 飘絮絮絮丶
戳我,来吐槽~