红包
◆ [2024-12-22 星期日 10:52] ◆ 您来自:18.188.245.152,欢迎您访问風雲工作室。 收藏本站 ◆ | ◆ 设为首页
联系站长(腾讯QQ)
5029111 [風雲]
站长当前离线
首  页 论坛交流 游戏频道 无忧脚本 旧版论坛 云服务器 聊 天 室 自助链接 来访记录 访客留言 搜索提供
位置:風雲工作室 - 论坛交流 - 技术专栏 - 后台开发 - 【推荐】SQL2005 中的行列转换(pivot) 返回
主题:【推荐】SQL2005 中的行列转换(pivot)
Rimifon
★☆☆☆☆☆☆☆☆☆
积分: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
该例子将“姓名”、“课程”、“分数”三列转换成了“姓名”,“语文”、“数学”、“物理”四列


时间:2011年5月6日 15:45:18 IP:已记录 引用 回复

© Copyright 2006-2024,風雲工作室 All rights reserved.
湘ICP备05009306号QQ登录
操作 1 个库,连接 2 次,执行 7 次,耗时 47 毫秒。