查询Oracle分组查询,最大值的记录?

来源:百度知道 编辑:UC知道 时间:2024/05/18 03:52:14
有一个表,三个字段,ID,种类,更新时间。

现在我想查询,按“种类”字段分组,查最大的“更新时间”的ID,能写出这样的查询语句吗?
ID的先后和更新时间字段没有必然联系。

这个题目真虎人,我还专门建了个表给你做的:
select * from test a where a.updatetime=(select max(b.updatetime) from test b where a.type=b.type)

呵呵 你试一下吧。

你意思是查询出最大时间的ID吗?可以这样实现:

select id from zzz where time = (select max(time) from zzz)

如果你要实现的是查询出每个种类中最大的更新时间的id,看看是否要这样的结果:

select id,type,max(time) from zzz where exists (select max(time) from zzz group by type) group by type,id

select *
from 表名 A,
(select 种类,max(更新时间) as 最后更新 from 表名 group by 种类) B
where A.种类=B.种类
and A.更新时间=B.最后更新

上面语句保证达到你的要求,但不一定最优。

select * from biao group by zl having max(gxsj)
你试试,不保证能用