Oracle:如何获得列表中每个项目的最大值
Oracle: How to get max value for each item in list
我有一个非常大的 table 需要从中检索数据。 table 看起来像这样:
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 查询是低效的。
这也导致进程内存不足错误。
有没有一种方法可以 运行 在单个查询中做到这一点?
我试过了:
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)
但是每个 select 查询只给我一个最大值(我明白为什么)
我真正想要的是 50000 个最大值。
如果我使用
,有没有办法为列表中的每个项目获取 max(B) 的值
select max(B) from table where A in (...)
谢谢!
看起来你只需要使用 GROUP BY
,像这样:
select A, max(B)
from table
group by A
order by A
如果我遗漏了什么,请告诉我。
我有一个非常大的 table 需要从中检索数据。 table 看起来像这样:
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 查询是低效的。 这也导致进程内存不足错误。
有没有一种方法可以 运行 在单个查询中做到这一点? 我试过了:
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)
但是每个 select 查询只给我一个最大值(我明白为什么) 我真正想要的是 50000 个最大值。
如果我使用
,有没有办法为列表中的每个项目获取 max(B) 的值select max(B) from table where A in (...)
谢谢!
看起来你只需要使用 GROUP BY
,像这样:
select A, max(B)
from table
group by A
order by A
如果我遗漏了什么,请告诉我。