SQL SERVER中apply操作符
您现在的位置:首页>教程>编程开发>mssql数据库 > SQL SERVER中apply操作符 SQL SERVER中apply操作符 感谢 3lian8 的投递 时间:2014-03-10 来源:三联教程 apply操作符 使用 APPLY 运算符可以为实现查询操作的外部表表达式返回的每个行调用表值函数。表值函
您现在的位置:首页 > 教程 > 编程开发 > mssql数据库 > SQL SERVER中apply操作符
SQL SERVER中apply操作符
感谢 3lian8 的投递 时间:2014-03-10 来源:三联教程
apply操作符
使用 APPLY 运算符可以为实现查询操作的外部表表达式返回的每个行调用表值函数。表值函数作为右输入,外部表表达式作为左输入。通过对右输入求值来获得左输入每一行的计算结果,生成的行被组合起来作为最终输出。APPLY 运算符生成的列的列表是左输入中的列集,后跟右输入返回的列的列表。
基础准备创建测试表:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
现在用APPLY操作符仅获取每个name的两个最高score记录:
?
1
2
3
4
5
select * from test4 a
cross apply
(
select top 2 * from test4score where test4id=a.id order by score desc
) b
分析如下:
右输入-- select top 2 * from test4score where test4id=a.id order by score desc
左输入--select * from test4
右输入求值对左输入的每一行进行计算。
更进一步分析:
左输入第一行是1 LeeWhoeeUniversity
右输入计算左输入第一行id最高两个score记录得出:
id test4id score
1 1 100
3 1 90
组合行:
id name test4id score
1 LeeWhoeeUniversity 1 100
1 LeeWhoeeUniversity 1 90
以此类推,直至完成左输入所有行的计算。
结果如下:
id name test4id score
1 LeeWhoeeUniversity 1 100
1 LeeWhoeeUniversity 1 90
2 LeeWhoee 2 90
2 LeeWhoee 2 82
outer apply 类似于LEFT JOIN,
?
1
2
3
4
5
select * from test4 a
outer apply
(
select top 2 * from test4score where test4id=a.id order by score desc
) b
id name test4id score
1 LeeWhoeeUniversity 1 100
1 LeeWhoeeUniversity 1 90
2 LeeWhoee 2 90
2 LeeWhoee 2 82
3 DePaul NULL NULL
由于test4score表中没有'DePaul'的记录,所以用NULL值填充。
当然还有更多的方法来实现此需求,如使用排名函数ROW_NUMBER:
?
1
2
3
4
select b.name,a.score from(
select *,ROW_NUMBER()over(partition by test4id order by score desc) as rum from test4score
) a
inner join test4 b on b.id=a.test4id where rum
结果:
name score
LeeWhoeeUniversity 100
LeeWhoeeUniversity 90
LeeWhoee 90
LeeWhoee 82
此方法是用前面介绍的ROW_NUMBER()和PARTITION BY来实现,详细请见:
SQL SERVER排名函数RANK,,DENSE_RANK,NTILE,ROW_NUMBER还有一种更古老的方法,但是必须给test4socre表添加标识列,新表结构如下:
?
1
2
3
4
5
6
create table test4Score
(
id int identity(1,1),
test4id int,
score int
)
新数据:
id test4id score
1 1 100
2 1 90
3 1 90
4 1 80
5 2 90
6 2 82
7 2 10
用带子查询的SQL语句:
?
1
2
3
4
select a.name,b.score from test4 a inner join test4score b on a.id=b.test4id where b.id in
(
select top 2 id from test4score where test4id=b.test4id order by score desc
)
结果:
name score
LeeWhoeeUniversity 100
LeeWhoeeUniversity 90
LeeWhoee 90
LeeWhoee 82
相关文章
标签:
[返回三联首页] [返回mssql数据库栏目] / [加入三联文集]

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PyCharm is a very popular Python integrated development environment (IDE). It provides a wealth of functions and tools to make Python development more efficient and convenient. This article will introduce you to the basic operation methods of PyCharm and provide specific code examples to help readers quickly get started and become proficient in operating the tool. 1. Download and install PyCharm First, we need to go to the PyCharm official website (https://www.jetbrains.com/pyc

sudo (superuser execution) is a key command in Linux and Unix systems that allows ordinary users to run specific commands with root privileges. The function of sudo is mainly reflected in the following aspects: Providing permission control: sudo achieves strict control over system resources and sensitive operations by authorizing users to temporarily obtain superuser permissions. Ordinary users can only obtain temporary privileges through sudo when needed, and do not need to log in as superuser all the time. Improved security: By using sudo, you can avoid using the root account during routine operations. Using the root account for all operations may lead to unexpected system damage, as any mistaken or careless operation will have full permissions. and

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

LinuxDeploy operating steps and precautions LinuxDeploy is a powerful tool that can help users quickly deploy various Linux distributions on Android devices, allowing users to experience a complete Linux system on their mobile devices. This article will introduce the operating steps and precautions of LinuxDeploy in detail, and provide specific code examples to help readers better use this tool. Operation steps: Install LinuxDeploy: First, install

Presumably many users have several unused computers at home, and they have completely forgotten the power-on password because they have not been used for a long time, so they would like to know what to do if they forget the password? Then let’s take a look together. What to do if you forget to press F2 for win10 boot password? 1. Press the power button of the computer, and then press F2 when turning on the computer (different computer brands have different buttons to enter the BIOS). 2. In the bios interface, find the security option (the location may be different for different brands of computers). Usually in the settings menu at the top. 3. Then find the SupervisorPassword option and click it. 4. At this time, the user can see his password, and at the same time find the Enabled next to it and switch it to Dis.

With the popularity of smartphones, the screenshot function has become one of the essential skills for daily use of mobile phones. As one of Huawei's flagship mobile phones, Huawei Mate60Pro's screenshot function has naturally attracted much attention from users. Today, we will share the screenshot operation steps of Huawei Mate60Pro mobile phone, so that everyone can take screenshots more conveniently. First of all, Huawei Mate60Pro mobile phone provides a variety of screenshot methods, and you can choose the method that suits you according to your personal habits. The following is a detailed introduction to several commonly used interceptions:

Apple brought some Pro-exclusive hardware features to iPhone 15 Pro and 15 Pro Max, which attracted everyone’s attention. We're talking titanium frames, sleek designs, the new A17 Pro chipset, an exciting 5x telephoto lens, and more. Of all the bells and whistles added to the iPhone 15 Pro models, the action button remains a prominent and prominent feature. Needless to say, it is a useful addition to launching actions on your iPhone. That said, you could accidentally hold down the Action button and trigger the feature inadvertently. Frankly, it's annoying. To avoid this, you should disable the action button on iPhone 15 Pro and 15 Pro Max. let

CSS web page scroll monitoring: monitor web page scroll events and perform corresponding operations. With the continuous development of front-end technology, the effects and interactions of web pages are becoming more and more rich and diverse. Among them, scroll monitoring is a common technology that can perform some special effects or operations based on the scroll position when the user scrolls the web page. Generally speaking, scroll monitoring can be implemented through JavaScript. However, in some cases, we can also achieve the effect of scroll monitoring through pure CSS. This article will introduce how to implement scrolling of web pages through CSS
