根据官方Microsoft BOL DENSE_RANK是不确定的(RANK())。但是根据Itzik Ben-Gan的排名函数, “ ... RANK()和DENSE_RANK()函数始终是确定性的”。谁是对的?
到目前为止,我发现的是: Microsoft的定义 “确定性函数在每次使用一组特定的输入值并被赋予相同的数据库状态时,总是返回相同的结果。”
所以在理论表中,员工
Employee Salary
Sue Right 1.00
Robin Page 1.00
Phil Factor 1.00
和员工2
Employee Salary
Phil Factor 1.00
Sue Right 1.00
Robin Page 1.00
是相同的。但是排名函数返回不同的值:
CREATE TABLE [dbo].[Employees](
--[ID] [int] IDENTITY(1,1) NOT NULL,
[Employee] [varchar](150) NOT NULL,
[Salary] [smallmoney] NULL,
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Employees2](
--[ID] [int] IDENTITY(1,1) NOT NULL,
[Employee] [varchar](150) NOT NULL,
[Salary] [smallmoney] NULL,
) ON [PRIMARY]
INSERT INTO [dbo].[Employees]
([Employee] ,[Salary])
VALUES
('Sue Right', 1)
, ('Robin Page', 1)
,('Phil Factor', 1 )
GO
INSERT INTO [dbo].[Employees2]
([Employee] ,[Salary])
VALUES
('Phil Factor', 1 )
,('Sue Right', 1)
,('Robin Page', 1)
GO
SELECT RANK() OVER ( ORDER BY Salary) AS [Rank]
, DENSE_RANK() OVER (ORDER BY Salary ) AS [Dense_rank]
, [Employee]
FROM
dbo.Employees
SELECT RANK() OVER ( ORDER BY Salary) AS [Rank]
, DENSE_RANK() OVER (ORDER BY Salary ) AS [Dense_rank]
, [Employee]
FROM
dbo.Employees2
SELECT NTILE(3) OVER ( ORDER BY SALARY )
, [Employee]
FROM
dbo.Employees
SELECT NTILE(3) OVER ( ORDER BY SALARY )
, [Employee]
FROM
dbo.Employees2