+---------+-------------------+------+------+
| 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
H
。H
总共可以等于70. H
只能有17.5分。 M
。 M
= 20/1 = 20。L
。H
)+ 20(来自M
)+ 0(来自L
)= 55。期望的输出
+---------+----------+-------+
| NAME | TITLE | SCORE |
+---------+----------+-------+
| A | Hippo | 55 |
| B | Snail | 100 |
| C | Dog | 90 |
+---------+----------+-------+
你如何在MySQl中进行这种类型的复杂评分/变量处理
答案 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;