--排名函数 ROW_NUMBER() RANK(), DENSE_RANK(),NTILE()
Over()函数 为排名函数生成排序顺序、
select ROW_NUMBER() over(order by OrderDate) as Romber,SalesOrderID,OrderDate from SalesLT.SalesOrderHeader ---创建一个单一的排序 SELECT ROW_NUMBER() OVER(partition by year(OrderDate),Month(OrderDate) order by orderdate) as rownumber, SalesOrderID,OrderDate,ShipMethod FROM SalesLT.SalesOrderHeader WHERE SalesOrderID>=280 ORDER BY OrderDate; ---ROW_NUMBER() SELECT rowNumber ,SalesOrderNumber SalesOrderID,OrderDate from ( select ROW_NUMBER() over(order by orderdate,SalesOrderID) as rowNumber,* from SalesLT.SalesOrderHeader where SalesOrderID>293 ) as Q where rowNumber between 21 and 40 order by rowNumber; --Rank() DENSE_RANK() EG:例如 张三跟王五 都是第二名 那么他们的Rank的值应为2 select productID,COUNT(*) as 'Count' from SalesLT.SalesOrderDetail group by ProductID order by COUNT(*); select Productid,salescount,RANK() over(order by salescount) as 'Ranks', DENSE_RANK() OVER(ORDER BY salescount) AS 'Dense_rank' from (select ProductID,COUNT(*) as salescount from SalesLT.SalesOrderDetail group by ProductID) as Q group by 'Ranks';
运行结果:对比
通过 对比:Rank的结果与 DeseRank的结果不一样。。