公式の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