SQL partition (小组排序)

很多时候,我们在SQL中进行数据去重(distinct)

结果发现有2条一样ID,或者name的数据,我们想要最接近的那条数据。


直接看看题目:

原表

select ID,Title,PRICE from Movies



现在我们想筛选 相同title下,金额最大的:

select * from (
select id,ROW_NUMBER() over(partition by title order by price desc) rid,title,price from Movies)A
where A.rid=1 
order by A.id


PS: 主要是运用 partition by 来进行小分组。

按照某个字段将一组数据进行分组

原文地址:https://www.cnblogs.com/hanjun0612/p/9779840.html