背景
计算同时满足多个条件情况下的值,并进行求和
奖项等级 | 奖项级别 | 分值 |
一等奖 | 省级 | 6 |
二等奖 | 省级 | 5 |
三等奖 | 省级 | 4 |
优秀奖 | 省级 | 3 |
一等奖 | 校级 | 3 |
二等奖 | 校级 | 2 |
三等奖 | 校级 | 1 |
优秀奖 | 校级 | 1 |
幸运奖 | 校级 | 0.5 |
需求
计算出每个人的加分总分。
脚本如下
SELECT t.`num`,t.`user_name`,t2`class`,t2`grade`,SUM(t.`score`) AS total_score
FROM
(SELECT t1.`num`,t1.`user_name`,
CASE
WHEN t1.`奖项级别`='省级' AND t1.`奖项等级`='一等奖' THEN 6
WHEN t1.`奖项级别`='省级' AND t1.`奖项等级`='二等奖' THEN 5
WHEN t1.`奖项级别`='省级' AND t1.`奖项等级`='三等奖' THEN 4
WHEN t1.`奖项级别`='省级' AND t1.`奖项等级`='优秀奖' THEN 3
WHEN t1.`奖项级别`='校级' AND t1.`奖项等级`='一等奖' THEN 3
WHEN t1.`奖项级别`='校级' AND t1.`奖项等级`='二等奖' THEN 2
WHEN t1.`奖项级别`='校级' AND t1.`奖项等级`='三等奖' THEN 1
WHEN t1.`奖项级别`='校级' AND t1.`奖项等级`='优秀奖' THEN 0.5
END AS 'score'
FROM t_activity_list_score_info_20230801to20230731 t1
) t, t_user_info t2
WHERE t.`num`=t2`num`
GROUP BY t.`num`,t.`user_name`,t2`grade`
ORDER BY SUM(t.`score`) DESC;