最大值的查询不能获得一行

时间:2018-03-09 09:19:42

标签: sql oracle greatest-n-per-group

我有一张表格,我想在哪里获得一行最大值。 我正在尝试:

select max(version) as max, curr_ver, creation_date, nameJob  from schema.def where nameJob = 'try'
group by curr_ver,creation_date,nameJob;

我明白了:

VERSION  CREATION_DATE  NAMEJOB CURR_VER
-------  -------------- -------- ---------
2        20180301       TRY      N
1        20180307       TRY      Y

我只想得到(最大行)

VERSION  CREATION_DATE  NAMEJOB CURR_VER
-------  -------------- -------- ---------
2        20180301       TRY      N

你知道我做错了什么吗? 谢谢,抱歉我的英文!

1 个答案:

答案 0 :(得分:1)

如果您希望row的值具有max组最大值,那么这样的事情就可以了:

select max(version) as max, curr_ver, creation_date, nameJob  
from schema.def 
where nameJob = 'try'
group by curr_ver,creation_date,nameJob
having 
max(version) = (select max(version) as global_max from schema.def 
where nameJob = 'try');