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

sql行列互转(转帖)

--行列互转
/******************************************************************************************************************************************************
以学生成绩为例子,比较形象易懂
整理人:中国风(Roy)
日期:2008.06.06
******************************************************************************************************************************************************/
--1、行互列
--> --> (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
--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 when [Course]='语文' then [Score] else 0 end)
from
Class
group by [Student]
GO
--2005方法:
动态:
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


相关文档:

SQL递归两张数据表

部门结构
Id name parentId
-----------------
1 人事部 0
2
开发部 1
3 服务部 1
用户结构
Id name departId
--------------------
101
张三 2
102 李四 2
103 王五 3
想得到
ID name
parentId
-------------------
1 人事部 0
2 开发部 1
101
张 ......

获取SQL Server元数据方法

元数据简介
  元数据 (metadata) 最常见的定义为"有关数据的结构数据",或者再简单一点就是"关于数据的信息",日常生活中的图例、图书馆目录卡和名片等都可以看作是元数据。在关系型数据库管理系统 (DBMS) 中,元数据描述了数据的结构和意义。比如在管理、维护 SQL Server 或者是开发数据库应用程序的时候,我们经常要获 ......

理解SQL SERVER中所有者和架构的区别

理解SQL SERVER中所有者和架构的区别
SQL SERVER2005介绍了架构,架构相对于以前版本中的对象所有者。本文将解释这两者的区别,并希望能解开你至今仍对架构一点困惑。
对象所有者
要理解所有者和架构之间的区别,让我们先花点时间来复习一下对象的所有权。在SQL SERVER2000或以前版本中创建一个对象,对象必须要有一个所 ......

SQL Server 2000的数据库容量

-->目录
-->SQL Server 构架
-->实施细则
-->最大容量说明
 
最大值(数量或大小)
对象 SQL Server 7.0 SQL Server 2000
批处理大小 65,536 * 网络数据包大小1 65,536 * 网络数据包大小1
每个短字符串列的字节数 8,000 8,000
每个 text、ntext、或 image 列的字节数 2 GB-2 2 GB-2
每个 GROU ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号