sql 在SQL查询中使用Rank

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 在SQL查询中使用Rank相关的知识,希望对你有一定的参考价值。

--Drop Sales.Quotas table if it exists  
 IF OBJECT_ID (N   'Sales.Quotas'   , N   'U'   )   IS    NOT    NULL  
 DROP    TABLE   Sales.Quotas  
 GO  
 --Create Sales.Quotas table  
 SELECT   e.FirstName, e.LastName, q.SalesQuota   AS   Quota,  
 DATENAME(m,q.QuotaDate)   AS   [   MONTH   ],   YEAR   (q.QuotaDate)   AS   [   YEAR   ]  
 INTO   Sales.Quotas  
 FROM   Sales.SalesPersonQuotaHistory q  
 INNER    JOIN   HumanResources.vEmployee e  
 ON   q.SalesPersonID   =   e.EmployeeID  
 WHERE   SalesQuota   BETWEEN   210000   AND   280000  
 ORDER BY   e.LastName, q.QuotaDate  

 /*  
 AS you can see, I simply pull DATA FROM a couple other tables IN the DATABASE IN order TO CREATE a SET OF meaningful test DATA.  

 Here's the SELECT statement I use to query the new table:  

 */  


 SELECT   
 ROW_NUMBER() OVER(ORDER BY Quota   DESC   )   AS   [RowNumber],  
 RANK() OVER(ORDER BY Quota   DESC   )   AS   [Rank],  
 DENSE_RANK() OVER(ORDER BY Quota   DESC   )   AS   [DenseRank],  
 NTILE(5) OVER(ORDER BY Quota   DESC   )   AS   [NTile],  
 LastName, Quota, [   MONTH   ], [   YEAR   ]  
 FROM   Sales.Quotas  

以上是关于sql 在SQL查询中使用Rank的主要内容,如果未能解决你的问题,请参考以下文章

使用rank/u rank的SQL Server查询示例

Hacker Rank SQL 问题:15 天的 SQL,如何使用 AND 选择查询

sql查询解释:oracle的rank和partition

SQL查询如何对增量键RANK中的相同结果进行单独分组

oracle复杂查询是sql

SQL 怎么用指定条件查询RANK函数生成的新排名列?