select出重复的数据,有没办法只保留最近的一条记录
select name, result , changedate from t
取出来的name可能有相同的,这种情况下有没办法根据changedate这条数据的插入时间,只取出最近的一条记录呢?
------解决方案--------------------select name,result,changedate from t a where not exists (
select name,result,changedate from t b where a.name=b.name and a.changedate<b.changedate)
------解决方案--------------------select name,result,changedate
from t
where (name,changedate) in (select name,max(changedate) from t group by name);
------解决方案--------------------
select *
from (select name,
result,
changedate row_number() over(partition by name order by changedate desc) as_level
from t) temp
where temp.as_level = 1
------解决方案--------------------select *
from (select name,
result,
changedate row_number() over(partition by name order by changedate desc) as_level
from t) temp
where temp.as_level = 1
这个oracle的分析函数 根据changedate 倒序排序 就去最近的一条
------解决方案--------------------over(partition by name order by changedate desc)
------解决方案--------------------使用over(partion by....) 此语法满足你的要求