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
相关文档:
原始数据
TERMINAL_ID
MAXDATE
TERMINAL_ID
OCCUR_DATE_TIME
TROUBLE_CD
1
12345
20100401102754
12345
20100401102754
210
2
12345
20100401102754
12345
20100401102754
211
3
12345
20100401102754
12345
20100401102754
?09
......
连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败 (Microsoft SQL Server,错误: 10060)
最开始你得确定客户端是否能ping通服务器端的ip地址,如果不能则请修改服务器端的防火墙的相应规则,使得客户端能够ping入,然后就开始进行下面的步骤。
步骤1.
打开SQL Server Configur ......
Entity sql 查询分析器
1、eSqlBlast for VS 2008 SP1 开源
download:http://code.msdn.microsoft.com/esql/Release/ProjectReleases.aspx?ReleaseId=991
用法:http://www.cnblogs.com/xiaomi7732/archive/2008/09/09/1287952.html
2、LINQPad
主页 http://www.linqpad.net/
不仅支持 entity sql ,还支持Linq ,s ......
排名函数是SQL Server2005新加的功能。在SQL Server2005中有如下四个排名函数:
1. row_number
2. rank
3. dense_rank
4. ntile
一、row_number
row_number函数的用途是非常广泛,这个函数的功能是为查询出来的每一行记录生成一个序号。row_number函数的用法如下面的SQL语句所示:
sel ......
/*
功能:利用函数创建流水号如:
fx201005260001,
fx201005260002,
fx201005270001
作者:陈永建
创建时间:2010-05-26
*/
use master
go
i ......