計劃
- 通過匹配
- 計算平均得分加入用戶分數平均分數和由用戶ID
計算衍生差字段的總和設置
create table scores
(
UserID integer not null,
MatchId integer not null,
Score decimal(5, 2) not null,
primary key (UserID, MatchId)
);
insert into scores
(UserID, MatchId, Score)
values
(1, 1, 22.1),
(2, 1, 36.0),
(3, 1, 35.3),
(1, 2, 50.0),
(2, 2, 39.8),
(3, 2, 42.0)
;
查詢
select s.UserID, sum(s.Score - avgs.avg_score) as paa
from scores s
inner join
(
select MatchId, avg(Score) as avg_score
from scores
group by MatchId
) avgs
on s.MatchId = avgs.MatchId
group by s.UserID
;
輸出
+--------+-----------+
| UserID | paa |
+--------+-----------+
| 1 | -2.966666 |
| 2 | 0.733334 |
| 3 | 2.233334 |
+--------+-----------+
sqlfiddle
什麼樣的遊戲包括浮點值?即使體操使得帶有小數做 – Strawberry
你應該表現出你的第一次嘗試也 –