【发布时间】:2018-12-26 10:05:38
【问题描述】:
我有一个表 per_all_Assignments_f 与 date_from 和 date_to 以及以下列结构:
PERSON_ID DATE_FROM DATE_TO GRADE
--------- ------------ ----------- -----
12 01-Jan-2018 28-Feb-2018 c
12 01-Mar-2018 29-Mar-2018 a
12 30-Mar-2018 31-dec-4712 b
13 01-jan-2018 31-dec-4712 c
在上表中,我必须检索最新的成绩变化,即对于person_id '12',我必须检索两个记录行:30-mar-2018 到 31 dec 4712 是最新的和之前的一行。我可以为此使用什么功能?
已解决:
SELECT person_id,
asg.grade_id,
lag(asg.grade_id) Over (Partition By person_ID Order By start_date) as prev_ppg_line1,
lag(start_date) Over (Partition By person_ID Order By start_date)
as prev_ppg_effective_start_date,
start_date,
row_Number() Over (Partition By person_ID Order By effective_start_date) as rn
FROM asg_table asg
WHERE person_id = 12;
此查询将获取包含所有先前更改的 3 行。我只想获取最新的更改,而不使用 max on Effective start date
【问题讨论】:
-
你的意思是 select person_id , date_From , date_to , grade from per_all_Assignments_f order by date_From date_to desc
-
no moudiz 我的意思是如果成绩发生了变化,那么最新的行和前 1 行。所以对于上面的例子....第 2 行和第 3 行应该为第 12 人返回。因为从 3 月 30 日到现在,成绩已经从 a 变为 b
标签: sql oracle analytic-functions