ITPub博客

首页 > 应用开发 > IT综合 > first_value和last_value的用法

first_value和last_value的用法

原创 IT综合 作者:macheraters 时间:2009-12-07 17:18:38 0 删除 编辑

first_value和last_value的用法:返回结果集中排在第一位和最后一位的值.

SQL> desc student
Name Type Nullable Default Comments
---- ------------ -------- ------- --------
NO VARCHAR2(5)
NAME VARCHAR2(25)
KM VARCHAR2(25) Y
CJ NUMBER Y

SQL> select * from student t;

NO NAME KM CJ
----- ------------------------- ------------------------- ----------
001 张三 语文 81
001 张三 数学 86
001 张三 英语 75
002 李四 语文 78
002 李四 数学 85
002 李四 英语 78
003 王五 语文 90

7 rows selected

SQL> select name,km,cj,
2 first_value(cj) over (partition by name order by cj desc rows between unbounded preceding and unbounded following ) as first,
3 last_value(cj) over (partition by name order by cj desc rows between unbounded preceding and unbounded following ) as last
4 from student t
5 ;

NAME KM CJ FIRST LAST
------------------- ------------------------- ---------- ---------- ----------
李四 数学 85 85 78李四 语文 78 85 78
李四 英语 78 85 78
王五 语文 90 90 90张三 数学 86 86 75张三 语文 81 86 75
张三 英语 75 86 75

7 rows selected

使用分析函数时注意查询范围的使用,缺省的windowing范围是:range between unbounded preceding and current row

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/20887841/viewspace-1029430/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    6
  • 访问量
    10345