我想在相同gr_number和路线的基础上获得基于Updated_ts的最后一行
我正在使用以下查询。
select t.*
from (select st.gr_number,st.course,st.is_active_flg,st.status,st.updated_ts
,sum(case when st.status = 'COMPLETED' then 1 else 0 end) over (partition by st.gr_number) as completedCourse,
sum(case when st.status <> 'COMPLETED' and st.status <> 'APPLICATION' and st.is_active_flg = 'N' then 1 else 0 end) over (partition by st.gr_number) as IncompletCourse
from admission_log st
join course cr on cr.course_id=st.course
order by st.gr_number,st.course,st.updated_ts
) t
where completedCourse > 0 and IncompletCourse > 0;
此查询给我的结果为
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9VN0FhbC5wbmcifQ==” alt =“在此处输入图像描述”>
从上面的结果中,我只希望针对相同的gr_number和基于Updated_ts的课程的最后一个值
like
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9HNDJvYS5wbmcifQ==” alt =“在此处输入图像描述”>
请帮助
我认为您可以使用row_number()
:
select t.*
from (select st.gr_number, st.course, st.is_active_flg, st.status, st.updated_ts,
sum(case when st.status = 'COMPLETED'
then 1 else 0
end) over (partition by st.gr_number) as completedCourse,
sum(case when st.status <> 'COMPLETED' and st.status <> 'APPLICATION' and st.is_active_flg = 'N'
then 1 else 0
end) over (partition by st.gr_number) as IncompletCourse,
row_number() over (partition by st.gr_number order by updated_ts desc) as seqnum
from admission_log st join
course cr
on cr.course_id = st.course
) t
where completedCourse > 0 and IncompletCourse > 0 and
seqnum = 1;
我不认为需要您的where
过滤器,但我将其留在了里面。