SqlServer窗口函数

窗口函数的作用

窗口函数是对一组值进行操作,不需要使用group by子句对数据进行分组,还能够在同一行中同时返回基础行的列和聚合列。窗口函数,基础列和聚合列的查询都非常简单。

语法格式

窗口函数的语法格式如下:

over([partition by value_expression,...,[n]],)
  • partition by:分组
  • order by:排序

首先创建一张测试表:

CREATE TABLE [dbo].[Scores](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [StudentId] [int] NULL,
    [CSharp] [float] NULL,
    [SqlServer] [float] NULL,
    [C语言] [float] NULL
)

表中数据如下:
SqlServer窗口函数_第1张图片

应用实例

1、avg()求平均值:

/*用group by分组
 */
SELECT StudentId, CSharp, AVG(CSharp) AS '平均分' FROM Scores group by StudentId,CSharp

SqlServer窗口函数_第2张图片

/*用over()分组,group by可省略
 *partition by studentid,CSharp 表示按studentid,CSharp分组
 */
SELECT StudentId,CSharp,AVG(csharp) over(partition by studentid,CSharp) AS '平均分' from Scores

SqlServer窗口函数_第3张图片

/*用over()分组,group by可省略
 *默认情况下所有数据为同一分组
 */
SELECT StudentId, CSharp, AVG(CSharp) over() AS '平均分' FROM Scores 

SqlServer窗口函数_第4张图片

/*用over()分组,group by可省略
 *partition by StudentId表示按StudentId分组
 */
SELECT StudentId, CSharp, AVG(CSharp) over(partition by StudentId) AS '平均分' FROM Scores 

SqlServer窗口函数_第5张图片

2、cast()转换数据格式
语法格式:cast(原数据 as 新格式)

/*用group by分组
 *将avg(CSharp)转换为decimal(5,2)格式
 */
select StudentId,CSharp,cast(AVG(csharp) as decimal(5,2)) as'平均分' from Scores group by StudentId,CSharp

SqlServer窗口函数_第6张图片

/*用over()分组,group by可省略
 *partition by studentid,CSharp表示按studentid,CSharp分组
 *将avg(CSharp)转换为decimal(5,2)格式
 */
select StudentId,CSharp,cast(AVG(csharp) over(partition by studentid,CSharp) as decimal(5,2)) as'平均分' from Scores

SqlServer窗口函数_第7张图片

/*用over()分组,group by可省略
 *默认情况下所有数据为同一分组
 *将avg(CSharp)转换为decimal(5,2)格式
 */
select StudentId,CSharp,cast(AVG(csharp) over() as decimal(5,2)) as'平均分' from Scores

SqlServer窗口函数_第8张图片

/*用over()分组,group by可省略
 *partition by studentid表示按studentid分组
 *将avg(CSharp)转换为decimal(5,2)格式
 */
select StudentId,CSharp,cast(AVG(csharp) over(partition by studentid) as decimal(5,2)) as'平均分' from Scores

SqlServer窗口函数_第9张图片

3、row_number()创建列编号

select row_number() over(partition by StudentId order by CSharp asc) as rowNumber,StudentId,CSharp from Scores

SqlServer窗口函数_第10张图片

需求:按照岗位Job分类,查询出每种岗位的最高工资是多少、哪个人拿到了这份工资
示例数据如下:
SqlServer窗口函数_第11张图片

/*row_number()按照Job分类,然后根据Salary降序排列,这样RowNumber=1的行就是工资最高的那一条数据*/
select * from 
(select ROW_NUMBER()over(partition by Job order by Salary desc,Id asc)as RowNumber,EmployeeName,Job,Salary from Employee)a 
where RowNumber=1;

运行截图如下:
SqlServer窗口函数_第12张图片
4、rank()排序
返回结果集的分区内每行数据的顺序,行的排名是从1开始算。如果两个或多个行的数据相同,则每个关联行将得到相同的排名。

select rank() over(partition by StudentId order by CSharp),StudentId,CSharp from dbo.Scores

SqlServer窗口函数_第13张图片

select rank() over(order by CSharp),StudentId,CSharp from dbo.Scores

SqlServer窗口函数_第14张图片

你可能感兴趣的:(SQL基础)