2012-06-21 47 views
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.

+1

爲什麼你要存儲的排名,當你可以簡單地在運行時計算?如果您存儲排名,則每當任何行更改時都必須更新整個表。 –

+0

也請不要使用HTML等格式化輸入。只需使用'{}'按鈕突出顯示代碼或表格結果。否則清理是相當混亂的。 –

+0

你好,亞倫先生,評分的原因是因爲我們有成千上萬的記錄,我們需要在許多頁面的排名。關於HTML,我查找了選項並沒有找到,所以最終使用HTML。我會牢記這一點。謝謝。 – user374760

回答

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; 
+0

作品完美。謝謝。 – user374760

0

而不是

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

您應該使用

SELECT Rank() over (Order by book_score desc) 

這將解決您的問題。