模拟 Oracle 的 Rank() Over、Dense_Rank()、Row_Number() 和 Ntile() 四个排名函数(渔版)等 SDTA 老师来共同探讨
下面的是模拟 Rank() Over、Dense_Rank()、Row_Number() 排名函数(鱼版)
*!* 1. 构造样本表数据 : Total_Sales
Create Cursor Total_Sales (Id I Autoinc, Name V(8), Sales I)
Insert Into Total_Sales (Name, Sales) Values ("Solomon", 10)
Insert Into Total_Sales (Name, Sales) Values ("Jason" , 15)
Insert Into Total_Sales (Name, Sales) Values ("Errol" , 20)
Insert Into Total_Sales (Name, Sales) Values ("Garry" , 40)
Insert Into Total_Sales (Name, Sales) Values ("Bernard", 50)
Insert Into Total_Sales (Name, Sales) Values ("Barry" , 20)
Insert Into Total_Sales (Name, Sales) Values ("Stephen", 15)
*!* 方法 1:同号取大号 = Rank()
Select A1.Name, A1.Sales, Count(A2.Sales) As Sales_Rank ;
From Total_Sales A1, Total_Sales A2 ;
Where A1.Sales <= A2.Sales Or (A1.Sales = A2.Sales And A1.Name = A2.Name) ;
Group By A1.Name, A1.Sales ;
Order By A1.Sales Desc, A1.Name Desc
*!* 方法 2:同号取小号 = Rank()
Select A1.Name, A1.Sales, Count(A2.Sales) Sales_Rank ;
From Total_Sales A1, Total_Sales A2 ;
Where A1.Sales < A2.Sales Or (A1.Sales = A2.Sales And A1.Name = A2.Name) ;
Group By A1.Name, A1.Sales ;
Order By A1.Sales Desc, A1.Name Desc
*!* 方法 3:序数排名,排名值联号 = Row_Number()
Select Name, Sales, Recno() As Sales_Rank ;
From (Select * ;
From Total_Sales ;
Order By Sales Desc) Total_Sales
*!* 方法 4:Dense_Rank()
Select A1.Name, A1.Sales, Count(DISTINCT A2.Sales) Sales_Rank ;
From Total_Sales A1, Total_Sales A2 ;
Where A1.Sales < A2.Sales Or (A1.Sales = A2.Sales And A1.Name = A2.Name) ;
Group By A1.Name, A1.Sales ;
Order By A1.Sales Desc, A1.Name Desc
Return
下面的是模拟 Rank() Over、Dense_Rank()、Row_Number() 排名函数(鱼版)
*!* 1. 构造样本表数据 : Total_Sales
Create Cursor Total_Sales (Id I Autoinc, Name V(8), Sales I)
Insert Into Total_Sales (Name, Sales) Values ("Solomon", 10)
Insert Into Total_Sales (Name, Sales) Values ("Jason" , 15)
Insert Into Total_Sales (Name, Sales) Values ("Errol" , 20)
Insert Into Total_Sales (Name, Sales) Values ("Garry" , 40)
Insert Into Total_Sales (Name, Sales) Values ("Bernard", 50)
Insert Into Total_Sales (Name, Sales) Values ("Barry" , 20)
Insert Into Total_Sales (Name, Sales) Values ("Stephen", 15)
*!* 方法 1:同号取大号 = Rank()
Select A1.Name, A1.Sales, Count(A2.Sales) As Sales_Rank ;
From Total_Sales A1, Total_Sales A2 ;
Where A1.Sales <= A2.Sales Or (A1.Sales = A2.Sales And A1.Name = A2.Name) ;
Group By A1.Name, A1.Sales ;
Order By A1.Sales Desc, A1.Name Desc
*!* 方法 2:同号取小号 = Rank()
Select A1.Name, A1.Sales, Count(A2.Sales) Sales_Rank ;
From Total_Sales A1, Total_Sales A2 ;
Where A1.Sales < A2.Sales Or (A1.Sales = A2.Sales And A1.Name = A2.Name) ;
Group By A1.Name, A1.Sales ;
Order By A1.Sales Desc, A1.Name Desc
*!* 方法 3:序数排名,排名值联号 = Row_Number()
Select Name, Sales, Recno() As Sales_Rank ;
From (Select * ;
From Total_Sales ;
Order By Sales Desc) Total_Sales
*!* 方法 4:Dense_Rank()
Select A1.Name, A1.Sales, Count(DISTINCT A2.Sales) Sales_Rank ;
From Total_Sales A1, Total_Sales A2 ;
Where A1.Sales < A2.Sales Or (A1.Sales = A2.Sales And A1.Name = A2.Name) ;
Group By A1.Name, A1.Sales ;
Order By A1.Sales Desc, A1.Name Desc
Return
民工子弟学校22班团小组长阳光模特队长冲锋篮球队前锋小苹果合唱队领唱蓝天舞蹈队编舞