我有一个返回以下数据结构的查询:
Course Math Science Computer Chemistry
------ ---- ------- -------- ---------
CI101 98
CI101 87
CI101 78
CI101 102
CI101 23
CI101 56
CI101 89
CI101 45
如何使用 SQL 使其具有以下结构:
Course Math Science Computer Chemistry
------ ---- ------- -------- ---------
CI101 98 78 23 45
CI101 87 102 56
CI101 89
这是我尝试过的 SQL:
select course,
case
when subject = 'MATH' then
count
end as MATH,
case
when subject = 'SCIENCE' then
count
end as SCIENCE,
case
when subject = 'COMPUTER' then
count
end as COMPUTER,
case
when subject = 'CHEMISTRY' then
count
end as CHEMISTRY
from t_course
GROUP BY course, subject, count
表结构:
Course Subject Count
------ ------- -----
CI101 Math 98
....
提前谢谢。