假设这是表" transactions ":
first_type second_type value
m t1 2
a t2 2
a t3 2
b t1 6
g t4 4
b t2 2
r t4 3
m t4 2
g t1 2
b t4 6
a t4 17
在显示关联的'second_type'列时,我如何选择每个'first_type'的最大值,期望的结果是:
first_type second_type value
m t1 2
b t1 6
g t4 4
r t4 3
m t4 2
b t4 6
a t4 17
或者只保留最大值,省略'first_type'列具有相同值的其他行,如下所示:
first_type second_type value
m t1 2
g t4 4
r t4 3
b t4 6
a t4 17
我试图选择最大值,而分组'first_type',但我不能选择'第二种类型,因为Oracle不允许选择的东西不在组子句
您想要rank()
:
select first_type, second_type, value
from (select t.*,
rank() over (partition by first_type order by value desc) as seqnum
from t
) t
where seqnum = 1;
注意:这里使用rank()
,因为您的数据有关联,并且您需要所有的关联。
你可以使用where in with Max
select first_type, second_type, value
from my_table
where ( first_type, value) in (select first_type, max(value)
from my_table
group by first_type)