我有两个或两个以上的价值观,比如:
c1|c2 |c3 |c4
--+---+---+---
1 | Z | B | 29
2 | Z | B | 19
我想要一个c4值更大的条目:
1 | Z | B | 29
我试图在c2和c3的分组之后从c4查询最大值,但这不起作用。
最佳答案
特殊溶液
select distinct on (c2,c3) c1, c2, c3, c4
from the_table
order by c2,c3,c4 desc
Ansi SQL溶液
select c1,c2,c3,c4
from (
select c1,c2,c3,c4,
row_number() over (partition by c2,c3 order by c4 desc) as rn
from the_table
) t
where rn = 1;