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
相关文档:
原帖及讨论:http://bbs.bc-cn.net/dispbbs.asp?boardid=12&id=140292
* 最近因为开发活动需要,用上了Eclipse,并要求使用精简版的SQL数据库(即SQL Server 2005)来进行开发项目 *
1.准备工作: 准备相关的软件(Eclipse除外,开源软件可以从官网下载)
<1> .Microsoft   ......
配置源程序
附加数据库SQL Server 2005
(1)将TM\01\App_Data文件夹中的db_SIS.mdf和db_SIS_log.ldf文件拷贝到SQL Server 2005安装路径下的MSSQL.1\MSSQL\Data目录下。
(2)选择开始/程序/Microsoft SQL Server 2005/SQL Server Management Studio项,进入到“连接到服务器”页面,如图1.1所示。
图1.1&nbs ......
sql server数据库备份方案
2008-08-05 13:15
SQL Server 数据库备份方案
为了保证SQL Server 数据的安全,数据库管理员应定期备份数据库,在不同情况下应采用不同备份数据库备份策略,一方面维持数据的安全性,另一方面也可保持SQL Server 能顺畅运行。尽最大的努力减少由于数据的损坏对客户造成的损失。
概念
1) 备份 ......
string s = " 80,81,83,82";
string[] s1 = s.Split(',');
int[] p = new int[s1.Count()];
for (int i = 0; i < s1.Count( ......
原文出处:http://www.dingos.cn/index.php?topic=1874.0
定义和用法
CONVERT() 函数是把日期转换为新数据类型的通用函数。
CONVERT() 函数可以用不同的格式显示日期/时间数据
语法
CONVERT(data_type(length),data_to_be_converted,style)
data_type(length) 规定目标数据类型(带有可选的长度)。data_to_be_conv ......