0

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

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。

4

2 回答 2

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;
于 2012-06-21T23:23:10.747 回答
0

代替

SELECT Rank() over (Order by book_score desc, book_id)

你应该使用

SELECT Rank() over (Order by book_score desc)

那会解决你的问题。

于 2012-06-21T23:25:38.663 回答