使用ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)为查询结果添加序号列
row_number() over(partition by col1 order by col2)表示根据col1分组,在分组内部根据col2排序,而此函数计算的值就表示每组内部排序后的顺序编号
例如:
select ROW_NUMBER() OVER(ORDER BY Convey_Fee desc) as z,rank() OVER(ORDER BY Convey_Fee desc) as z,DENSE_RANK() OVER(ORDER BY Convey_Fee desc) as z,NTILE(4) OVER(ORDER BY Convey_Fee desc) as z,(select sum(1) from Consign_D where Convey_Fee<=a.Convey_Fee) num,(select sum(1) from Consign_D where Convey_Fee<a.Convey_Fee) num1,Convey_Fee from Consign_D a order by Convey_Fee desc
结果: