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
相关文档:
文章由来: 最近需要做这样的测试:Install the products on machine which case-insensitive SQL installed.
所谓case-insensitive SQL installed 指在数据库安装时选择排序规则时 需要选择大小写区别的规则。
排序规则简介:
MS是这样描述的:"在 Micr ......
1、导出到XMl select * from Brand for xml auto ,root('Brands')
<Brands>
<Brand BrandID="E584596D-4D66-4F2F-B6F7-71C3BEB4CA21" Name="inganico" />
<Brand BrandID="19B04451-DDC4-4CDF-BE30-CB4E703B27DA" Name="安付达" />
<Brand BrandID="3C6C8E12-7C4A-4F1 ......
select
WorksheetID,worker,WorkDate,merchantName ,merchantNo ,manager
, case when insCount>0 then '新装' else '' end InsStr
,case when repCount>0 then '换装' else '' end RepStr
,case when UnInsCount>0 then '撤机' else '' end UnInsStr
,case when FaultCount>0 then '故障处理' els ......
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作为后台数据库建网站的时候,会涉及到远程连接SQL Server服务器的问题,但是经常会出现连接失败的现象,现在把解决办法归纳一下,以作总结。
一 看ping 服务器IP能否ping通。
这个实际上是看和远程sql server 2000服务器的物理连接是否存在。如果不行,请检查网络,查看配置,当 ......