您的位置:首页 > 数据库

【转】SQLServer 行列互换

2009-03-22 05:21 405 查看
http://www.cnblogs.com/NewLand/archive/2007/09/03/880540.html

--行列互换

Use tempdb

Go

Create table test ( [name] char(10), km char(10) , cj int )

Go

--初始化数据

insert into test values('张三','语文',80)

insert into test values('张三','数学',86)

insert into test values('张三','英语',75)

insert into test values('李四','语文',78)

insert into test values('李四','数学',85)

insert into test values('李四','英语',78)

Go

select name,sum(case km when '语文' then cj end) as 语文,

sum(case km when '数学' then cj end) as 数学,

sum(case km when '英语' then cj end) as 英语

from test

group by name
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: