总结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属于关系型数据库. 关系模型 以二维表来描述 ...
随机推荐
- Vue 项目中添加全局过滤器以及全局混合mixin
可以在.vue文件中定义局部使用的过滤器 export default{ data(){ return [] }, filters:{ toUpperCase:function(value){ ret ...
- bzoj2159
树形dp+第二类斯特林数 又是这种形式,只不过这次不用伯努利数了 直接搞肯定不行,我们化简一下式子,考虑x^n的组合意义,是把n个物品放到x个箱子里的方案数.那么就等于这个i=1->n,sigm ...
- Eclipse -- 自动补齐设置和其他用法
1:自动补齐设置:最简单的修改方式是:Windows——>Preferences——>Java-->Editor-->Content Asist,在Auto activatio ...
- Entity FrameWork 5 增删改查 & 直接调用sql语句 ?
#region 1.0 新增 -void Add() /// <summary> /// 1.0 新增 /// </summary> static void Add() { / ...
- 洛谷 - P3786 - 萃香抱西瓜 - 状压dp
重构一下就过了,不知道之前错在哪里. #include<bits/stdc++.h> using namespace std; typedef unsigned long long ull ...
- html页面选择图片上传时实现图片预览功能
实现效果如下图所示 只需要将下面的html部分的代码放入你的代码即可 (注意引入jQuery文件和html头部的css样式,使用的是ajax提交) <!-- 需引入jQuery 引入样式文件 引 ...
- Codeforces 625B【KMP】
题意就是一个串在另一个串出现几次,但是字符不能重复匹配, 比如aaaaaaa aaaa的答案是1 思路: 本来写了个暴力过的,然后觉得KMP改改就好了,就让队友打了一个: #include < ...
- CTP 下单返回错误: 没有报单权限 和字段错误需要注意的问题
没有报单权限一般被认为期货公司没有开权限, 但是更多的问题是没有填写 BrokerId, InvestorId 下单字段错误注意一个容易忽略的地方: a. order 应该全部设为0, b. orde ...
- 第一篇 Nosql讲解之Redis,Memchche,MongoDb的区别
本篇文章主要介绍Nosql的一些东西,以及Nosql中比较火的三个数据库Redis.Memchache.MongoDb和他们之间的区别.以下是本文章的阅读目录 一.Nosql介绍 1.Nosql简介 ...
- PostgreSQL - 查询表结构和索引信息
前言 PostgreSQL的表一般都是建立在public这个schema下的,假如现在有个数据表t_student,可以用以下几种方式来查询表结构和索引信息. 使用\d元命令查看表字段信息和索引信息 ...