|
★☆☆☆☆☆☆☆☆☆ |
积分:285 |
发帖:317 |
登录:2024/3/26 |
注册:2006/6/28 |
|
(1楼)【推荐】SQL2005 中的行列转换(pivot)
测试SQL如下: select * from (
-- 测试表数据开始
select '张三' as [姓名], '语文' as [课程], 74 as [分数]
union select '张三' as [姓名], '数学' as [课程], 83 as [分数]
union select '张三' as [姓名], '物理' as [课程], 93 as [分数]
union select '李四' as [姓名], '语文' as [课程], 74 as [分数]
union select '李四' as [姓名], '数学' as [课程], 84 as [分数]
union select '李四' as [姓名], '物理' as [课程], 94 as [分数]
union select '王五' as [姓名], '语文' as [课程], 67 as [分数]
union select '王五' as [姓名], '数学' as [课程], 89 as [分数]
union select '王五' as [姓名], '物理' as [课程], 77 as [分数]
-- 测试表数据结束
) t1 pivot(max([分数]) FOR [课程] in ( [语文], [数学], [物理])) t2 该例子将“姓名”、“课程”、“分数”三列转换成了“姓名”,“语文”、“数学”、“物理”四列
|