使用枢轴运算符划分成绩总和
Divide the sum of grades using pivot operator
SELECT
Student, [English], [Mathematics], [Science], [Programming], [History]
FROM
(SELECT Student, Grades, Subject
FROM Grade_Report) AS SOURCETABLE
PIVOT
(SUM(Grades)
FOR Subject IN ([English], [Mathematics], [Science], [Programming], [History])
) AS PIVOTTABLE
输出:
Jamie 188.00 161.00 163.00 183.00 184.00
Jenny 175.00 173.00 174.00 172.00 172.00
Jerome 184.00 186.00 184.00 191.00 181.00
预期输出:
Jamie 94 80.50 81.50 91.50 92
Jenny 87.50 86.50 87 86 86
Jerome 92 93 92 95.50 90.50
每个学生每个科目可以有两个分数?
首先确保这一点
尝试使用平均值AVG(Grades)
SELECT
Student, [English], [Mathematics], [Science], [Programming], [History]
FROM
(SELECT Student, Grades, Subject
FROM Grade_Report) AS SOURCETABLE
PIVOT
(AVG(Grades)
FOR Subject IN ([English], [Mathematics], [Science], [Programming],
[History])
)
SELECT
Student, [English], [Mathematics], [Science], [Programming], [History]
FROM
(SELECT Student, Grades, Subject
FROM Grade_Report) AS SOURCETABLE
PIVOT
(SUM(Grades)
FOR Subject IN ([English], [Mathematics], [Science], [Programming], [History])
) AS PIVOTTABLE
输出:
Jamie 188.00 161.00 163.00 183.00 184.00
Jenny 175.00 173.00 174.00 172.00 172.00
Jerome 184.00 186.00 184.00 191.00 181.00
预期输出:
Jamie 94 80.50 81.50 91.50 92
Jenny 87.50 86.50 87 86 86
Jerome 92 93 92 95.50 90.50
每个学生每个科目可以有两个分数? 首先确保这一点
尝试使用平均值AVG(Grades)
SELECT
Student, [English], [Mathematics], [Science], [Programming], [History]
FROM
(SELECT Student, Grades, Subject
FROM Grade_Report) AS SOURCETABLE
PIVOT
(AVG(Grades)
FOR Subject IN ([English], [Mathematics], [Science], [Programming],
[History])
)