作业帮 > 综合 > 作业

SQL 语句 如何把多行数据放入一行显示 比如:

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/18 04:18:55
SQL 语句 如何把多行数据放入一行显示 比如:
SID FID MID Score
----------- ---------- ---------- -----------
1 1 T001 80
2 2 T001 72
3 3 T001 68
4 4 T001 88
5 1 T002 65
6 2 T002 98
7 3 T002 78
8 4 T002 72
9 1 T003 66
10 2 T003 76
如何得到
T001 80 72 68 88
T002 68 98 78 72
.
如果FID是固定的只有1,2,3,4,可以用下面的sql实现
select MID,
sum(case when fid = 1 then score else null end) score1,
sum(case when fid = 2 then score else null end) score2,
sum(case when fid = 3 then score else null end) score3,
sum(case when fid = 4 then score else null end) score4
from tablename
group by MID