Oracle选择最近的日期记录 [英] Oracle select most recent date record

查看:66
本文介绍了Oracle选择最近的日期记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据日期字段查找最新记录.当我在where子句中设置最新= 1时,我得到一个错误.如果可以的话请帮忙. DATE是我正在排序的字段.我已经尝试了Latest = 1和Latest ='1'

I am trying to find the most recent record based on a date field. When I set latest = 1 in the where clause, I get an error. Please help if possible. DATE is a the field I'm sorting by. I have tried both latest = 1 and latest = '1'

SELECT 
STAFF_ID,
SITE_ID,
PAY_LEVEL,
ROW_NUMBER() OVER (PARTITION BY STAFF_ID ORDER BY DATE DESC) latest

 FROM OWNER.TABLE
WHERE   END_ENROLLMENT_DATE is null 
AND latest = 1

推荐答案

您不能在WHERE子句中使用选择列表中的别名(因为 SELECT语句的评估顺序 )

you can't use aliases from select list inside the WHERE clause (because of the Order of Evaluation of a SELECT statement)

您还不能在WHERE子句中使用OVER子句-您可以在选择列表或ORDER BY子句中使用此子句指定分析函数." (引自 docs.oracle.com )

also you cannot use OVER clause inside WHERE clause - "You can specify analytic functions with this clause in the select list or ORDER BY clause." (citation from docs.oracle.com)

select *
from (select
  staff_id, site_id, pay_level, date, 
  max(date) over (partition by staff_id) max_date
  from owner.table
  where end_enrollment_date is null
)
where date = max_date

这篇关于Oracle选择最近的日期记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆