孤城落晖
幼苗
共回答了25个问题采纳率:92% 举报
如果表的STUDENT字段和SUBJECT字段为组合关键字,那么这样
select distinct student,
语文=isnull((select grade from student b where b.student=a.student and b.subject='语文' ),''),
数学=isnull((select grade from student c where c.student=a.student and c.subject='数学' ),''),
英语=isnull((select grade from student d where d.student=a.student and d.subject='英语' ),''),
from student a
否则,这样写更保险:
select distinct student,
语文=isnull((select max(grade) from student b where b.student=a.student and b.subject='语文' ),''),
数学=isnull((select max(grade) from student c where c.student=a.student and c.subject='数学' ),''),
英语=isnull((select max(grade) from student d where d.student=a.student and d.subject='英语' ),''),
from student a
1年前
追问
8
举报
孤城落晖
CREATE VIEW v_student AS select distinct student, 语文=isnull((select grade from student b where b.student=a.student and b.subject='语文' ),''), 数学=isnull((select grade from student c where c.student=a.student and c.subject='数学' ),''), 英语=isnull((select grade from student d where d.student=a.student and d.subject='英语' ),''), from student a GO 或 CREATE VIEW v_student AS select distinct student, 语文=isnull((select max(grade) from student b where b.student=a.student and b.subject='语文' ),''), 数学=isnull((select max(grade) from student c where c.student=a.student and c.subject='数学' ),''), 英语=isnull((select max(grade) from student d where d.student=a.student and d.subject='英语' ),''), from student a GO