Row_Number() 分组排序

  SELECT  a.id 
         ,a.cnname
         ,b.personid                  
         ,Row_number() OVER(partition BY personid ORDER BY releasetime DESC) num
         ,releasetime                
  from   ent_movie a  
         LEFT JOIN ent_movie_relate_person b ON a.id = b.movieid 	   				                             
  WHERE  a.typeid=1 and a.cnName is not null and a.cnName!='' 
         AND b.professionid = 1
         and a.releaseTime>=DATEADD(YY,-3,GETDATE()) and  a.releaseTime<=GETDATE()

语法形式:ROW_NUMBER() OVER(PARTITION BY COL1 ORDER BY COL2) 

解释:根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的) 


参考学习:http://www.jb51.net/article/29162.htm


你可能感兴趣的:(partition,by,sql分组组内排序编号)