易截截图软件、单文件、免安装、纯绿色、仅160KB

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


相关文档:

SQL SERVER 2005无法远程连接的问题

连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败 (Microsoft SQL Server,错误: 10060)
最开始你得确定客户端是否能ping通服务器端的ip地址,如果不能则请修改服务器端的防火墙的相应规则,使得客户端能够ping入,然后就开始进行下面的步骤。
步骤1.
打开SQL Server Configur ......

Sql server With

with HostDevice as (----商户主机
select TerminalID ,Deviceid hostDeviceid ,Device.ModelID,Device.SN HostSN,Device.MerchantID,Device.InstallAddress,Device.SoftVersion
from Device
join model HostM on Device.ModelID=HostM.ModelID and HostM.Category in(0,3,4,5,6,8)
--where TerminalID is not null
) ......

SQL Server 2005 中如何做全文检索

1、查看当前数据库的状态
       SELECT
DATABASEPROPERTY
('pubs'
,'IsFulltextEnabled'
)
 2、打开FullText功能
       sp_fulltext_databse
'enable'
       关闭此功能
     & ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号