Postgres CASE和Range给出了比例分数

时间:2017-03-31 19:25:37

标签: postgresql

我有一个看起来像这样的表:

table: move_score_total
ID | Move_score_total
1  | 5.7865
2 .| 11.543
3 .| 14.29734

等...... 我想运行一个提供此问题的查询(不关心列顺序):

table: move_score_total
ID | scaled_score | Move_score_total
1  | 200          |5.7865
2 .| 80           |11.543
3 .| 20           |14.29734

我是案例和范围的新手,但我认为这个查询可能很接近。我呢?或者我离开了,因为我得到的错误是:

ERROR:  syntax error at or near "numrange"
LINE 3: ...  WHEN move_score_total.move_score_total::numeric numrange[0...


SELECT move_score_total."ID", 
CASE move_score_total.move_score_total
   WHEN move_score_total.move_score_total numrange[0,8) THEN 200
   WHEN move_score_total.move_score_total numrange[8,10) THEN 100
   WHEN move_score_total.move_score_total numrange[10,12) THEN 80                                    
   WHEN move_score_total.move_score_total numrange[12,14) THEN 40    
   WHEN move_score_total.move_score_total numrange[14,16) THEN 20 
   WHEN move_score_total.move_score_total numrange[16,18) THEN 10 
   WHEN move_score_total.move_score_total numrange[18,20) THEN 1                                         
ELSE NULL
END AS scaled_score
FROM move_score_total;  

1 个答案:

答案 0 :(得分:1)

我会用这个外连接替换select "ID", scaled_score from move_score_total left outer join (values (numrange (0,8), 200), (numrange (8,10), 100), (numrange (10,12), 80), (numrange (12,14), 40), (numrange (14,16), 20), (numrange (16,18), 10), (numrange (18,20), 1) ) s (r, scaled_score) on move_score_total <@ r

mvn release:prepare/perform