SQL SERVER中apply操作符

本文介绍了SQL SERVER中的APPLY操作符,包括APPLY和OUTER APPLY的使用。通过示例展示了如何利用APPLY获取每个名称的最高分数记录,并与LEFT JOIN进行对比。此外,还提及了使用排名函数RANK、DENSE_RANK等实现相同功能的方法。
摘要由CSDN通过智能技术生成

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值