行列互转_整理贴3

中国风
博客专家认证
2008-06-14 05:03:12
加精
--行列互转
/******************************************************************************************************************************************************
以学生成绩为例子,比较形象易懂

整理人:中国风(Roy)

日期:2008.06.06
******************************************************************************************************************************************************/

--1、行互列
--> --> (Roy)生成測試數據

if not object_id('Class') is null
drop table Class
Go
Create table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)
Insert Class
select N'张三',N'语文',78 union all
select N'张三',N'数学',87 union all
select N'张三',N'英语',82 union all
select N'张三',N'物理',90 union all
select N'李四',N'语文',65 union all
select N'李四',N'数学',77 union all
select N'李四',N'英语',65 union all
select N'李四',N'物理',85
Go
--2000方法:
动态:

declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+' from Class group by [Student]')


生成静态:

select
[Student],
[数学]=max(case when [Course]='数学' then [Score] else 0 end),
[物理]=max(case when [Course]='物理' then [Score] else 0 end),
[英语]=max(case when [Course]='英语' then [Score] else 0 end),
[语文]=max(case when [Course]='语文' then [Score] else 0 end)
from
Class
group by [Student]

GO
动态:

declare @s nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course]
exec('select * from Class pivot (max([Score]) for [Course] in('+@s+'))b')

生成静态:
select *
from
Class
pivot
(max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b

生成格式:
/*
Student 数学 物理 英语 语文
------- ----------- ----------- ----------- -----------
李四 77 85 65 65
张三 87 90 82 78

(2 行受影响)
*/

------------------------------------------------------------------------------------------
go
--加上总成绩(学科平均分)

--2000方法:
动态:

declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+',[总成绩]=sum([Score]) from Class group by [Student]')--加多一列(学科平均分用avg([Score]))

生成动态:

select
[Student],
[数学]=max(case when [Course]='数学' then [Score] else 0 end),
[物理]=max(case when [Course]='物理' then [Score] else 0 end),
[英语]=max(case when [Course]='英语' then [Score] else 0 end),
[语文]=max(case when [Course]='语文' then [Score] else 0 end),
[总成绩]=sum([Score]) --加多一列(学科平均分用avg([Score]))
from
Class
group by [Student]

go

--2005方法:

动态:

declare @s nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course] --isnull(@s+',','') 去掉字符串@s中第一个逗号
exec('select [Student],'+@s+',[总成绩] from (select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a
pivot (max([Score]) for [Course] in('+@s+'))b ')

生成静态:

select
[Student],[数学],[物理],[英语],[语文],[总成绩]
from
(select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a --平均分时用avg([Score])
pivot
(max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b

生成格式:

/*
Student 数学 物理 英语 语文 总成绩
------- ----------- ----------- ----------- ----------- -----------
李四 77 85 65 65 292
张三 87 90 82 78 337

(2 行受影响)
*/

go

--2、列转行
--> --> (Roy)生成測試數據

if not object_id('Class') is null
drop table Class
Go
Create table Class([Student] nvarchar(2),[数学] int,[物理] int,[英语] int,[语文] int)
Insert Class
select N'李四',77,85,65,65 union all
select N'张三',87,90,82,78
Go

--2000:

动态:

declare @s nvarchar(4000)
select @s=isnull(@s+' union all ','')+'select [Student],[Course]='+quotename(Name,'''')--isnull(@s+' union all ','') 去掉字符串@s中第一个union all
+',[Score]='+quotename(Name)+' from Class'
from syscolumns where ID=object_id('Class') and Name not in('Student')--排除不转换的列
order by Colid
exec('select * from ('+@s+')t order by [Student],[Course]')--增加一个排序

生成静态:
select *
from (select [Student],[Course]='数学',[Score]=[数学] from Class union all
select [Student],[Course]='物理',[Score]=[物理] from Class union all
select [Student],[Course]='英语',[Score]=[英语] from Class union all
select [Student],[Course]='语文',[Score]=[语文] from Class)t
order by [Student],[Course]

go
--2005:

动态:

declare @s nvarchar(4000)
select @s=isnull(@s+',','')+quotename(Name)
from syscolumns where ID=object_id('Class') and Name not in('Student')
order by Colid
exec('select Student,[Course],[Score] from Class unpivot ([Score] for [Course] in('+@s+'))b')

go
select
Student,[Course],[Score]
from
Class
unpivot
([Score] for [Course] in([数学],[物理],[英语],[语文]))b

生成格式:
/*
Student Course Score
------- ------- -----------
李四 数学 77
李四 物理 85
李四 英语 65
李四 语文 65
张三 数学 87
张三 物理 90
张三 英语 82
张三 语文 78

(8 行受影响)
*/

其它行列显示方法
http://blog.csdn.net/roy_88/archive/2008/07/26/2715856.aspx
...全文
23633 189 打赏 收藏 转发到动态 举报
写回复
用AI写文章
189 条回复
切换为时间正序
请发表友善的回复…
发表回复
keguangli 2012-09-08
  • 打赏
  • 举报
回复
今天学习了,
songtongrun 2012-09-02
  • 打赏
  • 举报
回复
学习了
kingas2002 2012-08-31
  • 打赏
  • 举报
回复
很久没好好看论坛了,谢谢楼主分享。
btxp163 2012-08-24
  • 打赏
  • 举报
回复
Mark,灰常不错
blackwing1988 2012-06-20
  • 打赏
  • 举报
回复
来瞧瞧~不错哦
xiaojiao525 2012-06-17
  • 打赏
  • 举报
回复
支持一下
lkldiy 2012-05-30
  • 打赏
  • 举报
回复
受教了~
gongjian0628 2012-05-25
  • 打赏
  • 举报
回复
select Student '姓名', a.数学 '数学',a.物理 '物理',a.英语 '英语',a.语文 '语文'
,a.数学+a.物理+a.英语+a.语文 '总成绩'
from Class
pivot
(
max(score) for Course in (数学,物理,英语,语文)
) as a
  • 打赏
  • 举报
回复
这篇帖子很好,行列转化是在一些项目中的需求还是蛮多的,另外我在博客园中也发现了一篇类似的帖子


http://www.cnblogs.com/zhangzt/archive/2010/07/29/1787825.html


kqyx_cn 2012-05-03
  • 打赏
  • 举报
回复
好东西。
sunfor 2012-04-29
  • 打赏
  • 举报
回复
mark.........
sunfor 2012-04-29
  • 打赏
  • 举报
回复
mark
daishenyang 2012-04-19
  • 打赏
  • 举报
回复
都是好东西
画心 2012-02-10
  • 打赏
  • 举报
回复
学习了!支持LZ!
honghe521 2012-01-09
  • 打赏
  • 举报
回复
接分了,。,
johnson520 2011-12-15
  • 打赏
  • 举报
回复
可以使用SQLSERVER新增函数 PIVOT
乱诗 2011-12-12
  • 打赏
  • 举报
回复
哎呀呀,正好是我要找的,谢谢楼主了
fuxiaoyang13 2011-11-29
  • 打赏
  • 举报
回复
顶顶!!!!!
沉默的羔羊 2011-11-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 roy_88 的回复:]
SQL code
--行列互转
/******************************************************************************************************************************************************
以学生成绩为例子,比较形象易懂

整理人:中国风(Roy)

……
[/Quote]


拜读了Roy在2008年时候的一篇帖子,其中有提及到行列互转的事情,
其中,列转行可以进行排序,
那行专列可以排序么?

黄瓜黄瓜 2011-11-06
  • 打赏
  • 举报
回复
收藏..............
加载更多回复(165)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧