I have this date
DECLARE @tbl table (ID INT, Items VARCHAR(5))
INSERT INTO @tbl VALUES
(1,'A'),(1,'A'),(1,'A'),(1,'B'),(1,'D'),(2,'A'),(2,'A'),(2,'B'),
(2,'B'),(2,'B'),(3,'A'),(3,'A'),(3,'B'),(3,'B'),(3,'C'),(3,'C'),
(3,'C'),(3,'E'),(3,'F')
What I want is In ID 1 there are 3 distinct items A,B, and C, all duplicate like A is ranked 1, B and C do not have duplicate so ranked 2 and 3 respectively IN ID 2 there are 2 distinct items A and B, A is 1 and the first next item B will be 1 until will get to the last value of item B that will be 2 etc
Current output
ID Items RN
1 A 1
1 A 2
1 A 3
1 B 1
1 D 1
2 A 1
2 A 2
2 B 1
2 B 2
2 B 3
3 A 1
3 A 2
3 B 1
3 B 2
3 C 1
3 C 2
3 C 3
3 E 1
3 F 1
Desired Output
My current query is
SELECT
*
,ROW_NUMBER()OVER(PARTITION BY CAST(ID AS VARCHAR(2))+Items ORDER BY Items DESC) AS RN
FROM @tbl
ORDER BY ID,Items

