The difference for ROW_NUMBER/RANK/DENSE_RANK/NTILE
USE tempdb
GO
CREATE TABLE Table1
(
ID INT IDENTITY(1,1) NOT NULL,
UserID INT NOT NULL
)
GO
INSERT INTO Table1(UserID)
SELECT 1
UNION ALL
SELECT 1
UNION ALL
SELECT 3
UNION ALL
SELECT 4
UNION ALL
SELECT 5
GO
SELECT
UserID
,ROW_NUMBER()OVER(ORDER BY UserID) AS RowNumberID
,RANK()OVER(ORDER BY UserID) AS RankID
,DENSE_RANK()OVER(ORDER BY UserID) AS DenseRankID
,NTILE(3)OVER(ORDER BY UserID) AS NtileID
FROM Table1
SELECT
UserID
,ROW_NUMBER()OVER(ORDER BY UserID) AS RowNumberID
,RANK()OVER(ORDER BY UserID) AS RankID
,DENSE_RANK()OVER(ORDER BY UserID) AS DenseRankID
,NTILE(3)OVER(ORDER BY UserID) AS NtileID
FROM Table1
结果: