Suppose I have a table containing names & numbers.
If there's 2 rows with the exact same values, it will get the same rank.
How can I give them a different number, despite being the same value?
So for example instead of having my ranks like this - 1,2,3,3,4 (where items 3 & 4 have the same rank), I want my rank to look like this - 1,2,3,4,5
Edit-
Showing the row number doesn't help me, because as I said I want to rank the rows.
Example -
If I use the regular rank, this is what I'll get. What I want is that for User1 with the value 80, it'll show the rank of 2 and 3, not 2 & 2. Same for User2. Using row number obviously won't give me that result....
╔═══════╦═══════╦══════╗
║ Name ║ Value ║ Rank ║
╠═══════╬═══════╬══════╣
║ User1 ║ 90 ║ 1 ║
║ User1 ║ 80 ║ 2 ║
║ User1 ║ 80 ║ 2 ║
║ User1 ║ 70 ║ 3 ║
║ User2 ║ 100 ║ 1 ║
║ User2 ║ 90 ║ 2 ║
║ User3 ║ 90 ║ 2 ║
║ User3 ║ 80 ║ 3 ║
╚═══════╩═══════╩══════╝
Hope that was clear enough... Thanks!
答案 0 :(得分:3)
为什么你说ROW_NUMBER()
对你没有帮助。
DECLARE @tbl TABLE(Name VARCHAR(10), Value INT)
INSERT @tbl
SELECT 'User1' , 90 UNION ALL
SELECT 'User1' , 80 UNION ALL
SELECT 'User1' , 80 UNION ALL
SELECT 'User1' , 70 UNION ALL
SELECT 'User2' , 100 UNION ALL
SELECT 'User2' , 90 UNION ALL
SELECT 'User3' , 90 UNION ALL
SELECT 'User3' , 80
SELECT Name, Value
,ROW_NUMBER() OVER(PARTITION BY Name ORDER BY Name, Value) AS Rn
FROM @tbl
Name Value Rn
User1 70 1
User1 80 2
User1 80 3
User1 90 4
User2 90 1
User2 100 2
User3 80 1
User3 90 2
答案 1 :(得分:1)
It depends on your RMDBS:
The query would look like:
SELECT FIELD_1, FIELD_2, ROW_NUMBER() FROM TABLE_A
Hope that helps