到目前为止,我有以下查询,它可以完美地根据我们的内部分数对书籍进行排名。
UPDATE CER
SET CER.book_rank = Ranker.ranc
FROM book_ranks CER
INNER JOIN
(SELECT Rank() over (Order by book_score desc, book_id) as ranc, book_id
FROM book_ranks
WHERE Category = 'Fiction'
GROUP BY book_id, book_score
) Ranker
ON
CER.book_id = Ranker.book_id
代码工作正常,但没有处理案例。
输入:
bookName book_score
-------- ----------
book2 45
book3 35
book5 35
book7 35
book9 30
当前输出:
bookName book_score book_rank
-------- ---------- ---------
book2 45 1
book3 35 2
book5 35 3
book7 35 4
book9 30 5
必需输出:
bookName book_score book_rank
-------- ---------- ---------
book2 45 1
book3 35 2
book5 35 2
book7 35 2
book9 30 5
作为book3,book5,book7具有相同的分数,它们的等级应该相同,book9应该具有5的等级。
答案 0 :(得分:1)
尝试:
SELECT * FROM book_ranks CER
INNER JOIN
(SELECT Rank() over (Order by book_score desc )
-- only difference is here ----------------^^
as ranc, book_id
FROM book_ranks
WHERE Category = 'Fiction'
GROUP BY book_id, book_score
) Ranker
ON CER.book_id = Ranker.book_id;
答案 1 :(得分:0)
而不是
SELECT Rank() over (Order by book_score desc, book_id)
你应该使用
SELECT Rank() over (Order by book_score desc)
这可以解决您的问题。