采用分析函数row_number()
select * from
(
select a.*,row_number() over (partition by column1 order by column2 [desc]) as rn
from table1
) q
where rn = 1
其中,partition by 是根据column1字段来分组,再根据column2来排序(默认为升序),最终的结果会给一个排序行号row_number
取rn = 1为column2字段值最小的记录
若要取最大的,则order by column2 desc,然后取rn = 1即可
时间: 2024-10-11 16:52:15