oracle获取表前几行,Oracle查询以获取同一表中相关行的先前值

我有一张桌子学生,名字和评分年份明智.

Name Year Rating

Ram 2016 10

Sam 2016 9

Ram 2014 8

Sam 2012 7

我需要找到员工之前的评级,可能是去年或几年前.

查询应返回以下结果

Name Cur_rating_year_2016 Prev_rating

Ram 10 8

Sam 9 7

下面是insert和create的脚本

Create table Student (name varchar2(10), year number, rating number );

insert into student values('Ram' ,2016 ,10);

insert into student values('Sam' ,2016 ,9);

insert into student values('Sam' ,2012 ,7);

insert into student values('Ram' ,2014 ,8);

有没有办法使用选择查询来实现这一目标?

LAG is an analytic function. It provides access to more than one row

of a table at the same time without a self join. Given a series of

rows returned from a query and a position of the cursor, LAG provides

access to a row at a given physical offset prior to that position.

For the optional offset argument, specify an integer that is greater

than zero. If you do not specify offset, then its default is 1. The

optional default value is returned if the offset goes beyond the scope

of the window. If you do not specify default, then its default is

null.

SELECT stud_name AS name,

r_year AS year,

r_value AS rating,

lag(r_value, 1, NULL) OVER(PARTITION BY stud_name ORDER BY r_year) AS prev_rating

FROM stud_r

ORDER BY stud_name;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值