2016-04-09 35 views
2

搜索並嘗試了很多示例後,我無法弄清楚這一點。 希望你能幫助我。組內3種方式的更新排名

這是我的表測試:

+-------+-----------+----------+---------+---------+-------+ 
    |Id  | Class | Score | Rank1 | Rank2 | Rank3 | 
    +-------+-----------+----------+---------+---------+-------+ 
    |1  |  1  |  9 | 0 | 0 | 0 | 
    |2  |  1  |  9 | 0 | 0 | 0 | 
    |3  |  1  |  8 | 0 | 0 | 0 | 
    |4  |  1  |  7 | 0 | 0 | 0 | 
    |5  |  2  |  9 | 0 | 0 | 0 | 
    |6  |  2  |  8 | 0 | 0 | 0 | 
    |7  |  2  |  8 | 0 | 0 | 0 | 
    |8  |  2  |  7 | 0 | 0 | 0 | 
    |9  |  2  |  6 | 0 | 0 | 0 | 
    +-------+-----------+----------+---------+---------+-------+ 

我想是更新我的表測試用3種排名:

  1. = 1級=每類具有連續排名在積分排名(沒有雙倍)
  2. =排名2 =連續排名的每級連續排名(雙)
  3. =排名3 = BLE)

E.g:

+-------+-----------+----------+---------+---------+-------+ 
    |Id  | Class | Score | Rank1 | Rank2 | Rank3 | 
    +-------+-----------+----------+---------+---------+-------+ 
    |1  |  1  |  9 | 1 | 1 | 1 | 
    |2  |  1  |  9 | 2 | 1 | 1 | 
    |3  |  1  |  8 | 3 | 2 | 3 | 
    |4  |  1  |  7 | 4 | 3 | 4 | 
    |5  |  2  |  9 | 1 | 1 | 1 | 
    |6  |  2  |  8 | 2 | 2 | 2 | 
    |7  |  2  |  8 | 3 | 2 | 2 | 
    |8  |  2  |  7 | 4 | 3 | 4 | 
    |9  |  2  |  6 | 5 | 4 | 5 | 
    +-------+-----------+----------+---------+---------+-------+ 

注意:它必須是一個適用UPDATE語句中。

對於1,我發現(但無法弄清楚如何使一個UPDATE):

SET @prev := null; 
SET @cnt := 0; 
SELECT IF(@prev <> Class, @cnt := 1, @cnt := @cnt + 1) AS Rank, @prev := Class 
FROM Test 
ORDER BY Class; 

對於3.我已經發現(但無法弄清楚如何使它一個UPDATE):

SELECT a.Id, 
    a.Score, 
    a.Class, 
    count(b.Score)+1 as Rank 
FROM Test a left join Test b 
    on a.Score>b.Score and a.Class=b.Class 
GROUP BY a.Id, 
     a.Score, 
     a.Class; 

與十進制值基於分數時,我已經添加了結果。奇怪的排名出現:

SET @prev_class = 0,@class = 0,@prev_score = 0,@score = 0,@rank3 = 0,@count=0; 
UPDATE 1i SET 
    Score_pq_raw_rank = (@prev_class := IFNULL(@class,0)), 
    Score_pq_raw_rank = (@class := Profile_id), 
    Score_pq_raw_rank = (@prev_score := IFNULL(@score,-1)), 
    Score_pq_raw_rank = (@score := Score_pq_raw), 
    Score_pq_raw_rank = (
    CASE WHEN @prev_class != @class THEN @rank3 := 1 
     WHEN @prev_class = @class AND @prev_score = @score THEN @rank3 
     WHEN @prev_class = @class AND @prev_score != @score THEN @rank3:[email protected][email protected] 
    END), 
    Score_pq_raw_rank = (CASE WHEN @prev_class != @class THEN @count := 0 
     WHEN @prev_class = @class AND @prev_score = @score THEN @count := @count + 1 
     WHEN @prev_class = @class AND @prev_score != @score THEN @count := 0 
    END), 
    Score_pq_raw_rank = @rank3 
ORDER BY Profile_id ASC, Score_pq_raw DESC, Rowresult_id ASC; 

+--------------+--------------+------------+-------------------+ 
| Rowresult_id | Score_pq_raw | Profile_id | Score_pq_raw_rank | 
+--------------+--------------+------------+-------------------+ 
|   1 | 3.69054000 |   1 |     1 | 
|   2 | 0.10568000 |   1 |     2 | 
|   3 | -2.08058000 |   1 |     3 | 
|   4 | -2.07316000 |   1 |     3 | 
|   5 | -2.39066000 |   1 |     3 | 
|   6 | -10.23852000 |   2 |     3 | 
|   7 | -8.77718000 |   2 |     2 | 
|   8 | -7.38480000 |   2 |     1 | 
|   9 | -13.49128000 |   2 |     4 | 
|   10 | -19.36774000 |   2 |     5 | 
+--------------+--------------+------------+-------------------+ 
+1

你嘗試過什麼SQL語句rank3? – BLE

+0

通常不會存儲派生數據 – Strawberry

+0

我已更新我的問題 – Dev

回答

0

這裏是選擇,給你你想要的排名。

SELECT Id,Class,Score, 
     @prev_class := IFNULL(@class,0), 
     @class := Class, 
     @prev_score := IFNULL(@score,-1), 
     @score := Score, 

     CASE WHEN @prev_class != @class THEN @rank1 := 1 
      ELSE @rank1 := @rank1 + 1 
     END as rank1, 
     CASE WHEN @prev_class != @class THEN @rank2 := 1 
      WHEN @prev_class = @class AND @prev_score = @score THEN @rank2 
      WHEN @prev_class = @class AND @prev_score != @score THEN @rank2:[email protected]+1 
     END as rank2, 
     CASE WHEN @prev_class != @class THEN @rank3 := 1 
      WHEN @prev_class = @class AND @prev_score = @score THEN @rank3 
      WHEN @prev_class = @class AND @prev_score != @score THEN @rank3:[email protected][email protected] 
     END as rank3, 
     CASE WHEN @prev_class != @class THEN @count := 0 
      WHEN @prev_class = @class AND @prev_score = @score THEN @count := @count + 1 
      WHEN @prev_class = @class AND @prev_score != @score THEN @count := 0 
     END as count 
FROM Test 
ORDER BY Class ASC, Score DESC, Id ASC; 

,這裏是該會的工作UPDATE,我只是用SET等級-1多次因爲MySQL似乎並沒有讓您只需設置@variables不使用結果某些列...

SET @prev_class = 0,@rank1 = 0,@score = 0,@prev_score = 0,@class =0,@rank2=0,@rank3 =0,@count=0; 
UPDATE Test SET 
     rank1 = (@prev_class := IFNULL(@class,0)), 
     rank1 = (@class := Class), 
     rank1 = (@prev_score := IFNULL(@score,-1)), 
     rank1 = (@score := Score), 
     rank1 = (
     CASE WHEN @prev_class != @class THEN @rank1 := 1 
      ELSE @rank1 := @rank1 + 1 
     END), 
     rank2 = (
     CASE WHEN @prev_class != @class THEN @rank2 := 1 
      WHEN @prev_class = @class AND @prev_score = @score THEN @rank2 
      WHEN @prev_class = @class AND @prev_score != @score THEN @rank2:[email protected]+1 
     END), 
     rank3 = (
     CASE WHEN @prev_class != @class THEN @rank3 := 1 
      WHEN @prev_class = @class AND @prev_score = @score THEN @rank3 
      WHEN @prev_class = @class AND @prev_score != @score THEN @rank3:[email protected][email protected] 
     END), 
     rank3 = (CASE WHEN @prev_class != @class THEN @count := 0 
      WHEN @prev_class = @class AND @prev_score = @score THEN @count := @count + 1 
      WHEN @prev_class = @class AND @prev_score != @score THEN @count := 0 
     END), 
     rank3 = @rank3 
ORDER BY Class ASC, Score DESC, Id ASC 

sqlfiddle

更新等級-1僅

SET @prev_class = 0,@class = 0,@prev_score = 0,@score = 0,@rank1 = 0; 
UPDATE Test SET 
     rank1 = (@prev_class := IFNULL(@class,0)), 
     rank1 = (@class := Class), 
     rank1 = (@prev_score := IFNULL(@score,-1)), 
     rank1 = (@score := Score), 
     rank1 = (
     CASE WHEN @prev_class != @class THEN @rank1 := 1 
      ELSE @rank1 := @rank1 + 1 
     END) 
ORDER BY Class ASC, Score DESC, Id ASC; 

更新秩2只

SET @prev_class = 0,@class = 0,@prev_score = 0,@score = 0,@rank2 = 0; 
UPDATE Test SET 
     rank2 = (@prev_class := IFNULL(@class,0)), 
     rank2 = (@class := Class), 
     rank2 = (@prev_score := IFNULL(@score,-1)), 
     rank2 = (@score := Score), 
     rank2 = (
     CASE WHEN @prev_class != @class THEN @rank2 := 1 
      WHEN @prev_class = @class AND @prev_score = @score THEN @rank2 
      WHEN @prev_class = @class AND @prev_score != @score THEN @rank2:[email protected]+1 
     END) 
ORDER BY Class ASC, Score DESC, Id ASC; 

更新只

SET @prev_class = 0,@class = 0,@prev_score = 0,@score = 0,@rank3 = 0,@count=0; 
UPDATE Test SET 
     rank3 = (@prev_class := IFNULL(@class,0)), 
     rank3 = (@class := Class), 
     rank3 = (@prev_score := IFNULL(@score,-1)), 
     rank3 = (@score := Score), 
     rank3 = (
     CASE WHEN @prev_class != @class THEN @rank3 := 1 
      WHEN @prev_class = @class AND @prev_score = @score THEN @rank3 
      WHEN @prev_class = @class AND @prev_score != @score THEN @rank3:[email protected][email protected] 
     END), 
     rank3 = (CASE WHEN @prev_class != @class THEN @count := 0 
      WHEN @prev_class = @class AND @prev_score = @score THEN @count := @count + 1 
      WHEN @prev_class = @class AND @prev_score != @score THEN @count := 0 
     END), 
     rank3 = @rank3 
ORDER BY Class ASC, Score DESC, Id ASC; 
+0

這是fenominal! 現在,因爲我的一些數據庫需要rank1,其他等級2等我無法弄清楚如何分別更新每個排名。我已經削減了一些部分,但只更新了0.你能幫助我將它們切片到3個單獨的排名更新,或者也許以正確的方式指出我如何? – Dev

+0

你希望能夠像3個不同的更新一樣單獨更新每個等級? –

+0

如果這是可能的,通過這一些分貝這將是非常有益的 – Dev