我有一个表c
,其中包含以下列
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
其中ID
为最大值,SeqNo
为每个不同值C1的最大值C2
结果应该是:
seqNo
如果有人可以提供帮助,请
答案 0 :(得分:0)
您需要的只是具有MAX聚合函数的GROUP BY子句:
SELECT id, MAX(rev)
FROM YourTable
GROUP BY id
...
答案 1 :(得分:0)
SELECT C1, SeqNo, MAX(C2), ID
FROM T1
WHERE C1 = 1
GROUP BY SeqNo
ORDER BY SeqNo DESC
LIMIT 1;
答案 2 :(得分:0)
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
答案 3 :(得分:0)
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