Mysql 选择具有最大值的行

Mysql 选择具有最大值的行,mysql,db2,Mysql,Db2,我有一个带有以下列的表T1 C1 SeqNo C2 ID 1 1 10 abc 1 1 20 xyz 1 1 30 mn0 1 2 10 123 1 3 10 abc 1 3 20 xyz 2 1 10 a1c 2 2 10 x1z 2 2 20 m10 2 2 30 1k3 2 3 10

我有一个带有以下列的表
T1

C1  SeqNo   C2  ID 

1   1       10  abc
1   1       20  xyz
1   1       30  mn0
1   2       10  123
1   3       10  abc
1   3       20  xyz
2   1       10  a1c
2   2       10  x1z
2   2       20  m10
2   2       30  1k3
2   3       10  a1c
2   3       20  x1z
对于C1的每个不同值,我必须选择
ID
,其中
SeqNo
为最大值,
C2
为最大值

结果应该是:

1    3    20    xyz
2    3    20    x1z

如果有人能提供帮助,请

您只需要一个带有MAX aggregate函数的GROUP BY子句:

选择id,最大值(修订版)
从你的桌子上
按id分组
…在db2中

select C1, ID, Max(SeqNo) as SeqNo, Max(C2) as C2
from yourtable
where C1=1
group by C1, ID
order by 3 desc, 4 desc, 2 desc
fetch first rows only
DB2中的其他解决方案

select * from (
select f1.*, rownumber() over(order by SeqNo desc, C2 desc, ID desc) rang
from yourtable f1 where f1.C1=1
) tmp where tmp.rang=1

为什么要将其标记为
mysql
db2
?按seqno DESC排序,C2 DESC LIMIT 1您使用的是mysql还是db2?每个人的答案都不一样。
select * from (
select f1.*, rownumber() over(order by SeqNo desc, C2 desc, ID desc) rang
from yourtable f1 where f1.C1=1
) tmp where tmp.rang=1