MySQL如何处理这种类型的点系统?

时间:2012-03-02 00:31:08

标签: mysql

+---------+-------------------+------+------+
| NAME    | TITLE             | SIZE | RANK |
+---------+-------------------+------+------+
| A       | Hippo             | H    |  1   |
| A       | Hippo             | M    |  1   |
| A       | Hippo             | H    | N/A  |
| A       | Hippo             | H    |  1   |
| A       | Hippo             | H    | N/A  |
| B       | Snail             | H    |  1   |
| B       | Snail             | M    |  1   |
| B       | Snail             | L    |  1   |
| C       | Dog               | H    |  1   |
| C       | Dog               | M    |  1   |
+---------+-------------------+------+------+

从上面提取的确切数据示例

+---------+-------------------+------+------+
| A       | Hippo             | H    |  1   |
| A       | Hippo             | M    |  1   |
| A       | Hippo             | H    | N/A  |
| A       | Hippo             | H    |  1   |
| A       | Hippo             | H    | N/A  |
+---------+-------------------+------+------+

要点如下:

H = 70 points
M = 20 points
L = 10 points
  • Hippo的大小列数为H
  • H总共可以等于70.
  • 因此70/4 = 17.5
  • 这意味着每个H只能有17.5分。
  • 如果'Rank'存在,则获得积分。
  • Hippo只有两个'Rank'在场。
  • 因此河马获得(17.5)x(2)= 35分。
  • Hippo在'size'列中也有一个M
  • 因此M = 20/1 = 20。
  • 因此,河马获得20分。
  • Hippo的大小栏目中没有L
  • 因此没有得分。
  • 总计35(来自H)+ 20(来自M)+ 0(来自L)= 55。

期望的输出

+---------+----------+-------+
| NAME    | TITLE    | SCORE |
+---------+----------+-------+
| A       | Hippo    |   55  |
| B       | Snail    |  100  |
| C       | Dog      |   90  |
+---------+----------+-------+

你如何在MySQl中进行这种类型的复杂评分/变量处理

1 个答案:

答案 0 :(得分:1)

在复杂的子选择和连接之后,这是您期望的结果:

select c.name,c.title,sum(score) from (
select b.name, b.title, avg_point*needcount as score from (select name, title, size,    count(size),  (case when size='H' then 70 when size='M' then 20 when size='L' then 10 end )/ count(size)  as avg_point from points group by name,title,size ) a
join 
(select name, title, size, count(size) as needcount from points where rank group by name, title, size) b
on a.name = b.name
and a.title = b.title
and a.size = b.size) c group by c.name,c.title;