SQL SERVER中apply操作符
apply操作符
使用 APPLY 运算符可以为实现查询操作的外部表表达式返回的每个行调用表值函数。表值函数作为右输入,外部表表达式作为左输入。通过对右输入求值来获得左输入每一行的计算结果,生成的行被组合起来作为最终输出。APPLY 运算符生成的列的列表是左输入中的列集,后跟右输入返回的列的列表。
基础准备
创建测试表:
create table test4
(
id int identity(1,1),
name varchar(100)
)
create table test4Score
(
test4id int,
score int
)
insert into test4(name)
select 'LeeWhoeeUniversity'
union all
select 'LeeWhoee'
union all
select 'DePaul'
insert into test4score(test4id,score)
select 1,100
union all
select 1,90
union all
select 1,90
union all
select 1,80
union all
select 2,90
union all
select 2,82
union all
select 2,10
test4表中数据:
id name
1 LeeWhoeeUniversity
2 LeeWhoee
3 DePaul
test4score表中数据:
test4id score
1 100
1 90
1 90
1 80
2 90
2 82
2 10