2016-02-23 151 views
0

我有一个非常大的表,我需要从中检索数据。该表是这样的:Oracle:如何获得列表中每个项目的最大值

A  B   C  D 
1  foo   4  q 
1  fool   2  p 
1  fools  13  a 
2  pho   5  d 
3  phone  14  g 
3  phones  6  f 

我试图运行类似:

select max(B) from table where A = 1 union 
select max(B) from table where A = 2 union 
. 
. 
. 
select max(B) from table where A = 50000; 

我想是可以得到:

1 -> fools 
2 -> pho 
3 -> phones 

我有大约50,000条记录运行这个查询。 上述方法理论上可行(我尝试了一个小子集),但我认为对每个50000值都有一个选择查询是低效的。 这也导致进程内存不足错误。

有没有一种方法可以在单个查询中执行此操作? 我想:

select max(B) from table where A in (first group of 1000) union 
select max(B) from table where A in (1000...2000) union 
. 
. 
. 
select max(B) from table where A in (40000...50000) 

但是这给了我每选择查询只有一个最大值(我明白为什么) 我真正想要的是50000个最大值。

有我得到的,如果我使用

select max(B) from table where A in (...) 

谢谢名单MAX(B)每个项目的值的方式!

回答

0

它看起来像你只需要使用GROUP BY,像这样:

select A, max(B) 
from table 
group by A 
order by A 

让我知道如果我失去了一些东西。

+0

工作,谢谢! 我需要做的: '选择A,最大值(B) 从表 组由A 工会 选择A,最大值(B) 从表 组由A 为了通过A' – c3p0

相关问题