总结SQL Server窗口函数的简单使用
总结SQL Server窗口函数的简单使用
前言:我一直十分喜欢使用SQL Server2005/2008的窗口函数,排名函数ROW_NUMBER()尤甚。今天晚上我在查看SQL Server开发的相关文档,整理收藏夹发现了两篇收藏已久的好文,后知后觉,读后又有点收获,顺便再总结一下。
一、从一个熟悉的示例说起
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
WITH Record AS ( SELECT Row_Number() OVER (ORDER BY Id DESC) AS RecordNumber, Id, FirstName, LastName, Height, Weight FROM Person (NOLOCK) ) SELECT RecordNumber, (SELECT COUNT(0) FROM Record) AS TotalCount, Id, FirstName, LastName, Height, Weight FROM Record WHERE RecordNumber BETWEEN 1 AND 10 |
二、窗口函数
|
1
2
3
4
5
6
7
8
|
CREATE TABLE [StudentScore]( [Id] [int] IDENTITY(1,1) NOT NULL, [StudentId] [int] NOT NULL CONSTRAINT [DF_StudentScore_StudentId] DEFAULT ((0)), [ClassId] [int] NOT NULL CONSTRAINT [DF_StudentScore_ClassId] DEFAULT ((0)), [CourseId] [int] NOT NULL CONSTRAINT [DF_StudentScore_CourseId] DEFAULT ((0)), [Score] [float] NOT NULL CONSTRAINT [DF_StudentScore_Score] DEFAULT ((0)), [CreateDate] [datetime] NOT NULL CONSTRAINT [DF_StudentScore_CreateDate] DEFAULT (getdate())) ON [PRIMARY] |
|
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
32
33
34
35
36
37
38
39
40
41
42
43
44
|
--CourseId 2:语文 4:数学 8:英语--1班学生成绩INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (1,1,2,85)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (2,1,2,95.5)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (3,1,2,90)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (1,1,4,90)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (2,1,4,98)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (3,1,4,89)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (1,1,8,80)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (2,1,8,75.5)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (3,1,8,77)--2班学生成绩INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (1,2,2,90)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (2,2,2,77)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (3,2,2,78)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (4,2,2,83)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (1,2,4,98)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (2,2,4,95)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (3,2,4,78)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (4,2,4,100)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (1,2,8,85)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (2,2,8,90)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (3,2,8,86)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (4,2,8,78.5)--3班学生成绩INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (1,3,2,82)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (2,3,2,78)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (3,3,2,91)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (1,3,4,83)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (2,3,4,78)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (3,3,4,99)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (1,3,8,86)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (2,3,8,78)INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score)VALUES (3,3,8,97) |
窗口函数使用OVER函数实现,OVER函数分带参和不带参两种。其中可选参数PARTITION BY用于将数据按照特定字段分组。
|
1
2
3
4
5
6
7
8
9
10
11
|
SELECT --Id, --CreateDate, StudentId, ClassId, CourseId, Score, CAST(AVG(Score) OVER() AS decimal(5,2) )AS '语文平均分'FROM StudentScore WHERE CourseId=2 |
|
1
2
3
4
5
6
7
8
9
10
11
|
SELECT Id, CreateDate, StudentId, ClassId, CourseId, Score, CAST(AVG(Score) OVER(PARTITION BY ClassId ) AS decimal(5,2) )AS '语文平均分'FROM StudentScore WHERE CourseId=2 |
b、可以在表达式中混合使用基本列和聚合列
二、让人爱不释手的排名函数
|
1
2
3
4
5
6
7
8
9
10
11
|
SELECT Id,-- CreateDate, ROW_NUMBER() OVER(ORDER BY Score DESC) AS '序号', StudentId, ClassId, CourseId, ScoreFROM StudentScore WHERE CourseId=8 |
|
1
2
3
4
5
6
7
8
9
10
11
|
SELECT Id,-- CreateDate, RANK() OVER(ORDER BY Score DESC) AS '序号', StudentId, ClassId, CourseId, ScoreFROM StudentScore WHERE CourseId=8 |
b、不同的是,ROW_NUMBER函数为每一个值生成唯一的序号,而RANK函数为相同的值生成相同的序号。
上图中,两个86分的学生对应的序号都是3,而接着排在它们下面的序号直接变成了5。
|
1
2
3
4
5
6
7
8
9
10
11
|
SELECT Id,-- CreateDate, DENSE_RANK() OVER(ORDER BY Score DESC) AS '序号', StudentId, ClassId, CourseId, ScoreFROM StudentScore WHERE CourseId=8 |
如果分区的行数不能被 integer_expression 整除,则将导致一个成员有两种大小不同的组。按照 OVER 子句指定的顺序,较大的组排在较小的组前面。
|
1
2
3
4
5
6
7
8
9
10
11
|
SELECT Id,-- CreateDate, NTILE(6) OVER(ORDER BY ClassId DESC) AS '组编号', StudentId, ClassId, CourseId, ScoreFROM StudentScore WHERE CourseId=8 |
总结SQL Server窗口函数的简单使用的更多相关文章
- SQL Server授权购买简单介绍
SQL Server授权购买简单介绍 之前有同事问我,使用盗版序列号的SQL Server到底有没有性能限制,之前本人一直没有深入研究过,后来经过一番资料搜集和查证,汇总成这篇文章 微软的SQL Se ...
- (4.34)sql server窗口函数
关键词:sql server窗口函数,窗口函数,分析函数 如果分析函数不可用,那么可能是版本还不支持 Window Function 包含了 4 个大类.分别是: 1 - Rank Function ...
- SQL Server Profiler的简单使用
SQL Server Profiler可以检测在数据上执行的语句,特别是有的项目不直接使用sql语句,直接使用ORM框架的系统处理数据库的项目,在调试sql语句时,给了很大的帮助. 之前写了使用SQL ...
- SQL Server Service Broker 简单例子 (转)
SQL Server Service Broker服务体系结构 消息类型 — 定义应用程序间交换的消息的名称.还可以选择是否验证消息.约定 — 指定给定会话中的消息方向和消息类型.队列 — 存储消息. ...
- SQL Server Profiler的简单使用(监控mssql)
SQL Server Profiler可以检测在数据上执行的语句,特别是有的项目不直接使用sql语句,直接使用ORM框架的系统处理数据库的项目,在调试sql语句时,给了很大的帮助. 之前写了使用SQL ...
- mySql学习笔记:比sql server书写要简单
在学mySql.总的感觉,mySql与Sql Server差不多,语法都很象,但mySql也许是吸取了SQL SERVER的一些经验,SQL语句书写起来更加简单. 比如说,设置主键.索引,SQL SE ...
- SQL Server窗口函数:ROWS与RANGE
几乎每次我展示SQL Server里的窗口时,人们都非常有兴趣知道,当你定义你的窗口(指定的一组行)时,ROWS与RANGE选项之间的区别.因此在今天的文章里我想给你展示下这些选项的区别,对于你的分析 ...
- C#对于sql server数据库的简单操作
1.在用windows模式登陆sql server 数据库 简历一个student的数据库,然后新建查询: create table student ( id int auto_increment p ...
- SQL SERVER数据库的简单介绍
一.数据库技术的发展 数据库技术是应数据管理任务的需求而产生的,先后经历了人工管理.文件系统.数据库系统等三个阶段. 二.关系型数据库 SQL Server属于关系型数据库. 关系模型 以二维表来描述 ...
随机推荐
- Adventure Works 教程
多维建模(Adventure Works 教程) 欢迎使用 Analysis Services 教程. 本教程通过在所有示例中使用虚构公司 Adventure Works Cycles,说明如 ...
- hibernate Criteria中or和and的用法
/s筛选去除无效数据 /* detachedCriteria.add( Restrictions.or( Restrictions.like("chanpin", &qu ...
- Unity3D 开发ios时困扰多时游戏开始画面图片的分辨率
- 3-6局部变量的存储方式 & 3-7字符型字面值
基础数据类型变量的存储 重点介绍方法级的变量,局势局部变量 存储中怎么存储呢? int n=100; 在栈中开辟内存存储空间. n是内存空间的别名 3-7字符型字面值 单引号不能丢,必须是英文状态. ...
- console.log是异步么?
让我们来看一个例子: var a = {}; console.log(a); a.foo = 'foo'; 4 console.log(a); 但是问题来了:在chorme跟firfox一样么? 结果 ...
- C# sbyte[]转byte[]
http://stackoverflow.com/questions/2995639/sbyte-vs-byte-using-methodssbyte[] orig = ... byte[] arr ...
- STLstack,queue
今天一开始用C去摸栈和队列,差不多昨天早上也在摸,摸烦了就去搞DP然后DP也没搞好,就是很烦很烦!!!! 然后今天那些C的栈队列的步骤和名称熟的不要不要的,然而数据结构的c语言用指针,传递,简直麻烦, ...
- Unity3D教程:无缝地形场景切换的解决方法
http://www.unitymanual.com/6718.html 当我们开发一个大型项目的时候-会遇到这样的问题(地形场景的切换)这个只是字面意思-并不是重场景1的100 100 100坐标 ...
- Matplotlib 在绘画bar时, 鼠标响应点击 bar 的消息
官方教程: http://urania.udea.edu.co/sitios/astronomia-2.0/pages/descargas.rs/files/descargasdt5vi/Cursos ...
- [Xcode 实际操作]七、文件与数据-(20)CoreML机器学习框架:检测和识别图片中的物体
目录:[Swift]Xcode实际操作 本文将演示机器学习框架的使用,实现对图片中物体的检测和识别. 首先访问苹果开发者网站关于机器学习的网址: https://developer.apple.com ...