易截截图软件、单文件、免安装、纯绿色、仅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语句使用 一个题目涉及到的50个Sql语句

转载自:http://www.diybl.com/course/7_databases/sql/sqlServer/2009124/154621_2.html
一个题目涉及到的50个Sql语句
Student(S#,Sname,Sage,Ssex) 学生表
Course(C#,Cname,T#) 课程表
SC(S#,C#,score) 成绩表
Teacher(T#,Tname) 教师表
问题:
1、查询“001”课程比“002”课程成绩高的 ......

SQL SERVER中交和差的实现

create table aaa
(
id int primary key ,
name varchar(30) not null
)
create table bbb
(
id int primary key ,
name varchar(30) not null
)
--交
select * from aaa
where exists
(
select * from bbb where aaa.id=bbb.id and aaa.name = bbb.name
)
--差
select *
from bbb
where not exists
( ......

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 ......

国外空间Sql数据乱码问题

国外空间貌似对中文比较感冒 如果数据类型设计为 varchar 类型的话 存储的数据基本上是  "????"
很简单 将 varchar 类型 设计为 nvarchar 类型
create table cs
(
   txt1 nvarchar(50) null
)
insert into cs (txt1 ) values ('测试')  -- 入库时数据时 ????
insert into cs (txt ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号