SQL server2005中用pivot实现行列转换
--> --> (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
动态:
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 行受影响)
*/
--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 whe
相关文档:
1、常用日期方法(下面的GetDate() = '2006-11-08 13:37:56.233')
(1)DATENAME ( datepart ,date )
返回表示指定日期的指定日期部分的字符串。Datepart详见下面的列表.
SELECT DateName(day,Getdate()) –返回8
(2)DATEPART ( datepart , date )
返回表示指定日期的指 ......
连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败 (Microsoft SQL Server,错误: 10060)
最开始你得确定客户端是否能ping通服务器端的ip地址,如果不能则请修改服务器端的防火墙的相应规则,使得客户端能够ping入,然后就开始进行下面的步骤。
步骤1.
打开SQL Server Configur ......
准备将一个excel表导入SQL Server2005中发生了下图的错误:
重启SQL Server2005还是出现上图的错误,解决方法(如下图):
在SQL Server Configuration Manager中将SSIS即SQL Server Integration Services的属性中的内置账户改为“本地系统”,重启服务即可导入excel了。 ......
/*
功能:利用函数创建流水号如:
fx201005260001,
fx201005260002,
fx201005270001
作者:陈永建
创建时间:2010-05-26
*/
use master
go
i ......