三个表(要求把学生每次考试情况列出来)
CREATE TABLE [dbo].[score](
[stuid] [int] NOT NULL,
[sid] [int] NOT NULL,
[score] [int] NOT NULL,
[examtimes] [int] NULL
)
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050701 , 1001 , 10 , 1 )
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050701 , 1002 , 20 , 1 )
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050701 , 1003 , 30 , 1 )
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050702 , 1001 , 11 , 1 )
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050702 , 1002 , 21 , 1 )
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050702 , 1003 , 31 , 1 )
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050703 , 1001 , 12 , 1 )
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050703 , 1002 , 22 , 1 )
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050703 , 1003 , 32 , 1 )
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050701 , 1001 , 110 , 2 )
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050701 , 1002 , 120 , 2 )
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050701 , 1003 , 130 , 2 )
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050702 , 1001 , 111 , 2 )
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050702 , 1002 , 121 , 2 )
INSERT [score] ( [stuid] , [sid] , [score] , [examtimes] ) VALUES ( 2010050702 , 1003 , 131 , 2 )
CREATE TABLE [Student] ( [studi] [int] NOT NULL , [stuname] [nvarchar] (50) NOT NULL )
INSERT [Student] ( [studi] , [stuname] ) VALUES ( 2010050701 , '张三' )
INSERT [Student] ( [studi] , [stuname] ) VALUES ( 2010050702 , '李四' )
INSERT [Student] ( [studi] , [stuname] ) VALUES ( 2010050703 , '王五' )
CREATE TABLE [Subject] ( [sid] [int] NOT NULL , [sname] [varchar] (50) NOT NULL )
INSERT [Subject] ( [sid] , [sname] ) VALUES ( 1001 , '语文' )
INSERT [Subject] ( [sid] , [sname] ) VALUES ( 1002 , '数学' )
INSERT [Subject] ( [sid] , [sname] ) VALUES ( 1003 , '英语' )
create view view_stu as --三个表Student(学生信息表一),Score(多对多关联表,有成绩字段多),Subject(课程名表一)
select a.stuname,c.sname,b.score,b.examtimes from Student a left join score b on a.studi=b.stuid
inner join Subject c on b.sid=c.sid group by a.stuname,b.examtimes,c.sname,b.score
select * from view_stu --查询视图,得到一个过滤后能做列和分组行的表
--此存储过程可通用,只需把视图和变量更改就可以
CREATE procedure CorssTab
@strTabName as varchar(50) = 'view_stu', --此处放表名
@strCol as varchar(50) = 'sname', --表头分组依据字段
@strGroup as varchar(50) = 'stuname',--分组字段
@strNumber as varchar(50) = 'score', --被统计的字段
@strSum as varchar(10) = 'Sum', --运算方式
@examtimes as varchar(10) = 'examtimes' --第几次考试
AS
DECLARE @strSql as varchar(1000), @strTmpCol as varchar(100)
EXECUTE ('DECLARE corss_cursor CURSOR FOR SELECT DISTINCT ' + @strCol + ' from ' + @strTabName + ' for read only ') --生成游标
begin
SET nocount ON
SET @strsql ='select ' + @strGroup + ',' + @examtimes + ', ' + @strSum + '(' + @strNumber + ') AS [' + @strSum + ' of ' + @strNumber + ']' --查询的前半段
OPEN corss_cursor
while (0=0)
BEGIN
FETCH NEXT FROM corss_cursor --遍历游标,将列头信息放入变量@strTmpCol
INTO @strTmpCol
if (@@fetch_status<>0) break
SET @strsql = @strsql + ', ' + @strSum + '(CASE ' + @strCol + ' WHEN ''' + @strTmpCol + ''' THEN ' + @strNumber + ' ELSE Null END) AS [' + @strTmpCol + ' ' + @strCol + ']' --构造查询
END
SET @strsql = @strsql + ' from ' + @strTabname + ' group by ' + @strGroup+','+ @examtimes --查询结尾
EXECUTE(@strsql) --执行
IF @@error <>0 RETURN @@error --如果出错,返回错误代码
CLOSE corss_cursor
DEALLOCATE corss_cursor RETURN 0 --释放游标,返回0表示成功
end
GO
EXEC CorssTab --运行存储过程
如果是固定列,就不用存储过程
select distinct t.name 姓名,t1.grade 语文,t2.grade 数学,t3.grade 英语
from course t,
(select c.name,c.grade from course c where c.subject='语文')t1,
(select c.name,c.grade from course c where c.subject='数学')t2,
(select c.name,c.grade from course c where c.subject='英语')t3
where t.name=t1.name
and t.name = t2.name
and t.name = t3.name
- 大小: 225.8 KB
分享到:
相关推荐
sql Server生成交叉表,可以用标准sql生成类似交叉表效果
SQL Server交叉表的客户端实现方案.pdf
基于SQL Server交叉表的学生成绩统计方法,季石磊,,在教学管理信息系统的开发过程中,灵活的学生成绩统计与分析是整个系统输出报表的关键问题之一,针对这一问题本文具体阐述了如何
SQL Server中交叉表查询的研究.pdf
sql server 交叉查询, 查询结果交叉表,横表变纵表
sqlserver 交叉表通用存储 资料引用:http://www.devdao.com/article/397559.htm
交叉表的语法,动态交叉的sql语句交叉表的语法,动态交叉的sql语句 交叉表的语法,动态交叉的sql语句交叉表的语法,动态交叉的sql语句
今天下午忙于将access数据库中的交叉表转到SQL server数据库中,在SQL server中,不支持transfrom方法,查找相关资料,找到使用case方法进行转换
复杂交叉表1.sql │ 多栏显示.sql │ 日期+星期+时间.sql │ 格式化报表.sql │ 横转竖-1.sql │ 横转竖-字段名.sql │ 横转竖-生成字段名.sql │ 横转竖.sql │ 行列互换的复杂...
sqlserver中 交叉表的使用 及遇到的一些小问题
在Sql Server数据库中利用存储过程实现动态交叉表.pdf
本书是Inside Microsoft SQL Server 2005系列四本著作中的一本。它详细介绍了T-SQL的内部体系结构,包含了非常全面的编程参考,提供了使用Transact-SQL(T-SQL)的专家级指导,囊括了非常全面的编程参考,揭示了基于...
详细介绍了SQL server数据库在存储过程中实现动态交叉表的实现方式和作用。
SQL Server数据库技术大全 电子书 图书目录: 第1篇 SQL Server基础 第1章 SQL Server 2008概述/2 1.1 SQL Server 2008简介/2 1.1.1 SQL Server发展历史/2 1.1.2 SQL Server 2008的特点/4 1.2 SQL Server 2008架构...
涉及安装和配置SQL Server、日期处理、字符处理、排序规则、编号处理、数据统计与汇总、分页处理、树形数据处理、数据导入与导出、作业、数据备份与还原、用户定义数据类型、数据库安全、系统表应用实例、SQL Server...
第1章 RDBMS基础:SQLServer数据库的构成 1.1 数据库对象概述 1.1.1 数据库对象 1.1.2 事务日志 1.1.3 最基本的数据库对象:表 1.1.4 文件组 1.1.5 数据库关系图 1.1.6 视图 1.1.7 存储过程 1.1.8 用户自定义函数 ...
SQL Server技术手册是对SQLServer数据库的详细介绍以及使用教程。中文讲解,内容详细,主要包含:DTS、触发器、内外函数、T-SQL、备份恢复、视图、游标、存储过程、全文检索、交叉表、事务、索引、设计、等等。
Excel 表分析工具:此外接程序提供了一些易于使用的工具,这些工具可利用 SQL Server 2008 的数据挖掘功能对电子表格数据执行强大的分析。此版本中添加了两个新工具:预测计算器和购物篮分析。 Excel 数据挖掘...
书名: SQLServer2008查询性能优化 作者: 弗里奇(Grant Fritchey) 出版社: 人民邮电出版社 出版日期: 2010年8月1日 ISBN: 9787115230294 编辑推荐 《SQL Server 2008查询性能优化》为你提供了处理查询性能所...
书名: SQLServer2008查询性能优化 作者: 弗里奇(Grant Fritchey) 出版社: 人民邮电出版社 出版日期: 2010年8月1日 ISBN: 9787115230294 编辑推荐 《SQL Server 2008查询性能优化》为你提供了处理查询性能所...