到目前为止,我有以下查询,它非常适合根据我们的内部分数对书籍进行排名。
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。