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 ......
declare @xml xml
set @xml = '<root/>'
select @xml
declare @value varchar(10)
set @value = 'val1'
set @xml.modify('insert <item value="{sql:variable("@value")}" /> into (/root)[1]')
select @xml
set @value = 'val2'
set @xml.modify('replace value of (/root/item/@value)[1] with "val2 ......
准备将一个excel表导入SQL Server2005中发生了下图的错误:
重启SQL Server2005还是出现上图的错误,解决方法(如下图):
在SQL Server Configuration Manager中将SSIS即SQL Server Integration Services的属性中的内置账户改为“本地系统”,重启服务即可导入excel了。 ......
快照隔离 Snapshot Isolation
1、写入程序不会阻碍读取程序
2、新的隔离级别提供了以下优点:
1) 提高了只读应用程序的数据可用性
2) 允许在OLTP环境中执行非阻止读取操作
3) 可对写入事务进行自动的强制冲突检测
3、演示代码
CREATE DATABASE demo2
GO
USE demo2
ALTER DATABASE demo2 SET allow_snapsho ......
数据库性能调优是每一个优秀SQL Server管理员最终的责任。虽然保证数据的安全和可用性是我们的最高的目标,但是假如数据库应用程序无法满足用户的要求,那么DBA们会因为性能低下的设计和实现而受到指责。SQL Server 2005在数据库性能方面得到了很多提高,尤其是表分区的技术。如果你还没不了解表分区的特征,那么请你花点 ......