使用函数PIVOT现实数据行转列
表数据库如下图所示:
要求按Course分类值,用列表示,同进要求查询结果如下图表示。
SELECT stu_Name, SUM([Mathematics]) as Mathematics, SUM([English]) as English, SUM([Physics]) as Physics,
SUM([History]) as History
FROM
(SELECT intID,stu_Name,Course,Grade
FROM tblStuGrade) p
PIVOT
(
sum (Grade)
FOR Course IN ( [Mathematics], [English], [Physics], [History] )
) AS pvt
group by stu_Name
SUM([History]) as History
FROM
(SELECT intID,stu_Name,Course,Grade
FROM tblStuGrade) p
PIVOT
(
sum (Grade)
FOR Course IN ( [Mathematics], [English], [Physics], [History] )
) AS pvt
group by stu_Name
posted on 2009-06-16 16:21 zengshunyou 阅读(296) 评论(0) 编辑 收藏 举报