我有下面的表,并试图运行以下查询以获得最新的DEPTID根据EFFDT,这是第二行的 (DAA System Infrastructur 1/13/2017),但我仍然得到所有的记录。我在这里做错了什么?Oracle - Sql查询获取表中的最新记录
我确实看过类似的问题,但没有像我一样的问题。
select d.deptid,d.descr,d.effdt
from SYSADM.PS_DEPT_TBL d
inner join(select deptid,descr,max(to_date(effdt)) as max_date
from SYSADM.PS_DEPT_TBL
group by deptid, descr) d1
on d.deptid = d1.deptid
and to_date(effdt) = max_date
where d.deptid ='DAA'
这是表:
DEPTID DESCR EFFDT
-------------------------------------------
DAA Telecommunications 2/18/2013
DAA System Infrastructure 1/13/2017
DAA Manager, Telecommunications 1/1/1900
DAA System Infrastructure & Contrl 7/8/2013
可以颠倒顺序,并采取先用'LIMIT 1' – SubjectDelta
'descr'不应该的一部分镂空表/内联视图,也不属于组的一部分。 – xQbert