我想请人帮助解决一个困扰我两天的问题.
我有这样的"结果"表:
result_id | competition_id | competitor_id | competitor_ranking |
---|---|---|---|
1 | 1 | 1 | 0.1 |
2 | 1 | 2 | 0.4 |
3 | 1 | 3 | 0.2 |
4 | 1 | 4 | 0.3 |
5 | 2 | 1 | 0.4 |
6 | 2 | 2 | 0.1 |
7 | 2 | 3 | 0.2 |
8 | 2 | 5 | 0.3 |
9 | 3 | 3 | 0.1 |
10 | 3 | 4 | 0.4 |
11 | 3 | 5 | 0.2 |
12 | 3 | 6 | 0.3 |
从"结果"表中,我想得到一个包括罚分(+1.0)的竞争者分组排名,如下所示:
competitor_id | competitions | rankings | ranking_with_penalties |
---|---|---|---|
1 | 1; 2; M | 0.1; 0.4 | 0.1; 0.4; +1.0 |
2 | 1; 2; M | 0.4; 0.1 | 0.4; 0.1; +1.0 |
3 | 1; 2; 3 | 0.2; 0.2; 0.1 | 0.2; 0.2; 0.1 |
4 | 1; M; 3 | 0.3; 0.4 | 0.3; +1.0; 0.4 |
5 | M; 2; 3 | 0.3; 0.2 | +1.0; 0.3; 0.2 |
6 | 3; M; M; | 0.3 | 0.3; +1.0; +1.0 |
我知道group_concat函数是一个聚合函数,它将所有非空值串联在一个列中.我知道这项任务很琐碎.但我解决不了.
CREATE TABLE results (
result_id INTEGER PRIMARY KEY,
competition_id INTEGER,
competitor_id INTEGER,
competitor_ranking
);
INSERT INTO results(competition_id, competitor_id, competitor_ranking) VALUES
(1, 1, 0.1), (1, 2, 0.4), (1, 3, 0.2), (1, 4, 0.3),
(2, 1, 0.4), (2, 2, 0.1), (2, 3, 0.2), (2, 5, 0.3),
(3, 3, 0.1), (3, 4, 0.4), (3, 5, 0.2), (3, 6, 0.3)
;
SELECT
competitor_id,
group_concat(coalesce(competition_id, NULL), '; ') AS competitions,
group_concat(coalesce(competitor_ranking, NULL), '; ') AS rankings,
group_concat(coalesce(NULLIF(competitor_ranking, NULL), '+1.0'), '; ') AS ranking_with_penalties
FROM results
GROUP BY competitor_id;
- M是一个符号,意味着一个竞争者错过了一场比赛,并遵循排名与处罚逻辑.我需要获得参赛者分组样本中错过的比赛,并将其替换为罚分(+1.0)以计算最终排名.一个参赛者应该参观的比赛.
- added the above from the comments
我期待着任何帮助.