sql server 2008中的apply运算符使用方法

所属分类: 数据库 / mssql2008 阅读数: 185
收藏 0 赞 0 分享

Apply运算符可以实现两个查询结果的全组合结果,又称为交叉集合。例如两个数据组合(A,B)、(A,B),他们的交叉集合为(AA,AB,AA,AB)。

Apply分为Cross Apply和Outer Apply两种使用方式。具体分析如下:

首先先建立两个表StudentList和ScoreInfo。脚本语言如下:

复制代码 代码如下:

create table StudentList(
id int Identity(1,1) not null,
Name nvarchar(20) not null,
Sex bit not null,
Birthday date not null,
Class nvarchar(2) not null,
Grade nvarchar(2) not null,
regdate date not null,
Primary key (id));

create table ScoreInfo(
id int Identity(1,1) not null primary key,
StudentID int not null,
ClassID int not null,
Score int not null,
TestDate date not null,
regdate date not null);


其中ScoreInfo中的StudentID为StudentList中id的外键

插入数据,脚本如下

复制代码 代码如下:

insert into StudentList(Name, Sex, Birthday, Class, Grade, regdate) values('张三', 1, '1988-05-28', 1, 8, '2010-05-05');

insert into StudentList(Name, Sex, Birthday, Class, Grade, regdate) values('李四', 1, '1985-09-13', 4, 4, '2010-05-05');

insert into StudentList(Name, Sex, Birthday, Class, Grade, regdate) values('王丽', 0, '1987-11-05', 1, 7, '2010-05-05');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(1, 1, 98, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(1, 2, 92, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(1, 3, 86, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(2, 1, 95, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(2, 2, 94, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(2, 3, 91, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(3, 1, 90, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(3, 2, 88, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(3, 3, 90, '2010-04-15', '2010-05-01');


两个表结构建立完毕,数据也成功插入进去了。为了便于讲解在StudentList表中再插入一条记录
复制代码 代码如下:

insert into StudentList(Name, Sex, Birthday, Class, Grade, regdate)
values('李铭', 1, '1989-05-04', 2, 7, '2010-05-05');

输入以下语句
复制代码 代码如下:

select * from StudentList a
cross apply
(select ClassID, Score from ScoreInfo where StudentID=a.id) b;

结果如下

再输入以下语句

select * from StudentList a
outer apply
(select ClassID, Score from ScoreInfo where StudentID=a.id) b;

结果如下

可以看出Cross Apply和Outer Apply的区别

Cross Apply把语句两边的两个Select查询结果进行交叉配对,将所有结果展示出来。Cross Apply查询确保在查询两个子集数据的交集时,只有有效信息的集合才被列出来。

OuterApply查询是把两个子集的所有组合列了出来,不管数据是否有交叉,全部显示要配对的数据。

更多精彩内容其他人还在看

mssql 监控磁盘空间告警实现方法

需要通过调用OLE 自动存储过程获取磁盘信息,而这些组件,基于服务器的安全配置,通常是禁用的,我们在存储过程通过sp_configure开启这个服务,调用服务完毕后,又通过sp_configure禁用该服务。另外,数据库服务器都位于内网,因此安全问题应该不大
收藏 0 赞 0 分享

SQL Server2008导出数据之Excel详细解析

我觉得数据库的导入和导出很有用,顺便做一下总结,以免将来有遗忘。需要的朋友可以过来参考下
收藏 0 赞 0 分享

利用JAVA实现DES加密算法

本文简单讲解DES的JAVA实现方法。需要的朋友可以过来参考下
收藏 0 赞 0 分享

SQL Server 2008怎样添加自增列实现自增序号

有的表需要添加自增列,在添加新纪录时自动添加一个序号,有两种不错的方法通过T-SQL代码、通过企业管理器在此分享给大家
收藏 0 赞 0 分享

SQLServer 2008 :error 40出现连接错误的解决方法

在与SQLServer建立连接时出现与网络相关的或特定与实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且SQL SERVER已配置允许远程链接
收藏 0 赞 0 分享

Java基本类型与包装类详细解析

存储方式及位置的不同,基本类型是直接存储变量的值保存在堆栈中能高效的存取,封装类型需要通过引用指向实例,具体的实例保存在堆中
收藏 0 赞 0 分享

SQL Server数据库管理员(DBA)的工作内容

DBA的工作目标就是确保Microsoft SQL Server 2008系统正常高效地运行。DBA的工作也是最繁忙的工作,无论是性能调整,还是灾难恢复,都离不开DBA的支持
收藏 0 赞 0 分享

如何在SQL Server 2008下轻松调试T-SQL语句和存储过程

sqlserver2008调试的要求和条件:如果在引擎所在的电脑或服务器上调试,则只需要SA或者WINDOWS用户登陆即可。如果是异地调试,则需要设置防火墙例外,增加SSMS和SQLSERVER.EXE为允许,增加135端口允许通过
收藏 0 赞 0 分享

解决SQL SERVER 2008数据库表中修改字段后不能保存

SQL SERVER 2008数据库表中修改字段后不能保存,这种情况将阻止保存要求重新创建表的更改一项的钩钩去掉就OK了
收藏 0 赞 0 分享

sqlserver多版本查看版本号

有人问我sqlserver怎么查看版本号呢?下面给大家整理了一些SQL Server版本号查看sql语句,可以查看SQL Server 2000 、2005 、2008版本号
收藏 0 赞 0 分享
查看更多