总结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属于关系型数据库. 关系模型 以二维表来描述 ...
随机推荐
- CentOS7.0安装Ceph(jewel)及以上版本
背景 由于docker的Ceph插件rexray对Ceph版本有一定的要求,当Ceph版本为hammer (0.94.10)时,rexray无法成功创建rbd设备.CentOS 7及以上版本,默认安装 ...
- Dynamic Gcd
树链剖分+差分 直接区间加显然是不行的,由于gcd(a,b,c)=gcd(a,a-b,b-c),那么我们对这些数差分,然后就变成单点修改.原本以为这道题很简单,没想到这么麻烦,就膜了发代码. 首先我们 ...
- Datawindow.net中实现让当前行选中,并且当前行以其他颜色显示
如何在Datawindow.net中实现让当前行选中,并且当前行以其他颜色显示 使用Datawindow.net要选中一行是很容易的意见事情,有很多种办法可以实现,最简单的莫过于使用selectrow ...
- 12_tcp_ip相关概念
java基础班 网络编程应该提过.大学学计算机或者是通信的对这些东西肯定比较熟一些.主机到网络层是跟硬件相关的一些协议了.上层协议得依赖下层的协议.也就是说它们得联合起来共同工作才能够把数据传输出去 ...
- 安装mosquitto报缺少dll文件的错误
解决:下载缺少的dll组件,放到安装目录. 报错:The procedure entry point CRYPTO_memcmp could not be located in the dynami ...
- 20个Flutter实例视频教程-第13节: 展开闭合案例
20个Flutter实例视频教程-第13节: 展开闭合案例 视频地址: https://www.bilibili.com/video/av39709290/?p=13 博客地址: https://js ...
- Git之常用的命令操作
set LESSCHARSET=utf-8 git设置文件名大小写敏感 git branch -rgit checkout 'TestMaster'git checkout -b 'TestMaste ...
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- 483. Smallest Good Base
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...
- Unity亚洲开发者大会会议简录之技术篇,Profiler的一些参数的意思
2014Unity亚洲开发者大会会议简录之技术篇 拖公司的福,有幸去了一趟北京参加了一场Unity3D的交流盛宴,在为期两天的时间内,不仅有着技术上收获,也有心灵上的震撼.现在先来说说技术方面的一些比 ...