gpt4 book ai didi

mysql - 带有主元的 SUM 来计算总分

转载 作者:行者123 更新时间:2023-11-29 08:34:48 25 4
gpt4 key购买 nike

从另一个问题中,我得到了这个查询来正确总结我的分数:

SELECT callSign,event, SUM(score) 
FROM scores LEFT JOIN candidates
ON scores.candidateID=candidates.id
WHERE candidateID IN
(SELECT id
FROM candidates
WHERE assessmentID='1321')
GROUP BY event, callSign
ORDER BY candidateID,event

我得到的数据如下:

callSign    event           TotalScore
Y209 Bridge 45
Y209 PSA 3
Y209 Team Analyst Exam 40
X125 PSA 1
X125 Team Analyst Exam 38
V023 Amazing Race Planning 37

我需要的是这样的数据:

callSign      Bridge   PSA   Amazing Race Planning     Team Analyst Exam   
V023 37
Y209 45 3 40
X125 1 38

表结构

`events`
id event
1 PSA
2 Bridge
30 Stress Board
25 Amazing Race Planning
26 Amazing Race Execution

`scores`
id candidateID event rubric category score comment
1 18 Team Analyst Exam Team Leader Rubric Organizes and Tasks Team Members 3
2 18 Team Analyst Exam Team Leader Rubric Roles and Responsibilities 5
3 18 Team Analyst Exam Team Leader Rubric Backward Planning 5
4 18 Team Analyst Exam Team Leader Rubric Time Management

`candidates`
id firstName middleInitial lastName callSign service rank sex height weight assessmentID currentlyAssessing hired

callSign 是 X125 所在的地方

最佳答案

由于您使用的是 MySQL,为了将数据转换为列,您需要使用带有 CASE 表达式的聚合函数:

SELECT callSign, 
SUM(case when event = 'Bridge' then score else 0 end) as Bridge,
SUM(case when event = 'PSA' then score else 0 end) as PSA,
SUM(case when event = 'Amazing Race Planning' then score else 0 end) As AmazingRacePlanning,
SUM(case when event = 'Team Analyst Exam' then score else 0 end) as TeamAnalystExam
FROM scores
LEFT JOIN candidates
ON scores.candidateID=candidates.id
WHERE candidateID IN (SELECT id
FROM candidates
WHERE assessmentID='1321')
GROUP BY callSign

如果您的事件数量未知,那么您将必须使用准备好的语句来生成动态 SQL:

SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'sum(CASE WHEN event = ''',
event,
''' THEN score END) AS `',
event, '`'
)
) INTO @sql
FROM scores
LEFT JOIN candidates
ON scores.candidateID=candidates.id;


SET @sql
= CONCAT('SELECT callSign, ', @sql, '
FROM scores
LEFT JOIN candidates
ON scores.candidateID=candidates.id
WHERE candidateID IN (SELECT id
FROM candidates
WHERE assessmentID=''1321'')
GROUP BY callSign');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

编辑#1,如果您的事件存储在单独的表中,那么您可以使用以下命令生成动态结果:

SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'sum(CASE WHEN event = ''',
event,
''' THEN score END) AS `',
event, '`'
)
) INTO @sql
FROM events;



SET @sql
= CONCAT('SELECT callSign, ', @sql, '
FROM scores
LEFT JOIN candidates
ON scores.candidateID=candidates.id
WHERE candidateID IN (SELECT id
FROM candidates
WHERE assessmentID=''1321'')
GROUP BY callSign');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

参见SQL Fiddle with Demo

关于mysql - 带有主元的 SUM 来计算总分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15502583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com