oracle如何获取最大时间的整条记录

cdisk 2012-02-29 02:37:59
Table
ID PID TIME UPDATETIME
----------------------------------------
1 2 20:11 2012-02-26
2 2 20:20 2012-02-27
3 2 20:20 2012-02-27
4 3 19:12 2012-02-27
5 3 18:16 2012-02-27
6 4 15:45 2012-02-28
7 4 15:30 2012-02-29
8 5 06:05 2012-02-29
-----------------------------------------
我想取得每个PID中的UPDATETIME字段最大值的数据不知道如何是好啊
下面这个语句也不行, 因为最大时间有时候对应两条记录呢

select * from Table a where a.UPDATETIME=(select max(UPDATETIME) from Table where PID=a.PID)

求教各位大神,帮助看看呀,
(好似我全部分都拿出来了~,大哥们行行好啊)
...全文
15197 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xpingping 2012-02-29
  • 打赏
  • 举报
回复
select pid,max(UPDATETIME ) from table group by pid

--或者
select pid,UPDATETIME
from (select pid,UPDATETIME,
row_number() over (partition by pid order by UPDATETIME desc) rn
from table )
where rn = 1
opps_zhou 2012-02-29
  • 打赏
  • 举报
回复

select *
from (select t.*, row_number() over (order by datetime desc) as rnum
from tbl t)
where rnum = 1;



如果,最大时间有好几条记录的话,可以修改 over() 里,对 order by 追加排序规则就可以了。
yinan9 2012-02-29
  • 打赏
  • 举报
回复
使用ROWID 取唯一记录
dawugui 2012-02-29
  • 打赏
  • 举报
回复
如果最大时间有时候对应两条记录的话,再以哪个为标准?
假设为id,取最大的哪个?

select t.* from tb t where not exists (select 1 from tb where pid = t.pid and (UPDATETIME > t.UPDATETIME or (UPDATETIME = t.UPDATETIME and id > t.id) ))
dawugui 2012-02-29
  • 打赏
  • 举报
回复
select t.* from tb t where UPDATETIME = (select max(UPDATETIME) from tb where pid = t.pid)

select t.* from tb t where not exists (select 1 from tb where pid = t.pid and UPDATETIME > t.UPDATETIME)
liyongjie 2012-02-29
  • 打赏
  • 举报
回复
select pid,max(updatetime) from test8 group by pid
这个不行吗?
hupeng213 2012-02-29
  • 打赏
  • 举报
回复
select * from table where to_date(UPDATETIME || time || ':00','yyyy-mm-dd hh24:mi:ss')=(
select max(to_date(UPDATETIME || time || ':00','yyyy-mm-dd hh24:mi:ss')) from table )

3,491

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧