2016-12-02 58 views
0

我发现了一些针对此问题的解决方案,但是,它们似乎不适用于Oracle。如何获得Oracle中每个组的最大值?

我得到这个:

enter image description here

我想以目前大约只有年龄最大的人每队的信息。所以,我的输出应该是这样的:

PERSON | TEAM | AGE 
Sam  | 1 | 23 
Michael | 2 | 21 

我怎么能在Oracle中做到这一点?

回答

2

一种方法是使用keep

select team, max(age) as age, 
     max(person) keep (dense_rank first order by age desc) as person 
from t 
group by team; 

还有其他的方法,但以我的经验,keep工作得很好。

+0

这种方法有效,但是有没有更简单的方法?老师从来没有教过我们“保持” – ItsLavishBitch

+0

我认为这是从你在这里看到的所有答案中最简单的方法。然而[FIRST](https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions056.htm)并不是那种众所周知的。 –

2

下面是一个例子,而不keeprow_number()

with t0 as 
(
    select person, team, age, 
    row_number() over(partition by team order by age desc) as rn 
    from t 
) 
select person, team, age 
from t0 
where rn = 1; 
0
select * from table 
where (team, age) in (select team, max(age) from table group by team)