SQL Server 2012 开窗函数
废话不多说了,直接给大家贴代码了,具体代码如下所示:
-- 开窗函数:在结果集的基础上进一步处理(聚合操作) -- Over函数,添加一个字段显示最大年龄 SELECT * , MAX(StuAge) OVER ( ) MaxStuAge FROM dbo.Student; -- Over函数,添加一个字段显示总人数 SELECT * , COUNT(StuID) OVER ( ) StuCount FROM dbo.Student; -- Partition By 分组统计数量 -- 根据性别分组后,统计 SELECT COUNT(*) OVER ( PARTITION BY StuSex ) , * FROM dbo.Student; -- 根据班级分组后,统计、排序 SELECT COUNT(*) OVER ( PARTITION BY Class ORDER BY Height) , * FROM dbo.Student; -- Over函数,添加一个字段显示平均身高 SELECT * , AVG(Height) OVER ( ) AgeHeight FROM dbo.Student; --Row_Rumber() SELECT ROW_NUMBER() OVER ( ORDER BY StuID DESC ) RowNumber , * FROM dbo.Student --Row_Rumber() 实现分页效果 ; WITH T AS ( SELECT ROW_NUMBER() OVER ( ORDER BY StuID DESC ) RowNumber , * FROM dbo.Student ) SELECT * FROM T WHERE T.RowNumber BETWEEN 1 AND 3; --Rank() 排名函数,名次相同,跳过 SELECT RANK() OVER ( ORDER BY Height ) , * FROM dbo.Student; --DENSE_Rank() 排名函数,名次相同不跳过 SELECT DENSE_RANK() OVER ( ORDER BY Height ) , * FROM dbo.Student; -- NTILE()函数,参数:记录总数/划分区域 = 每个区域数组,把记录序号放进数组 (平均分组) SELECT NTILE(3) OVER ( ORDER BY StuSex ) , * FROM dbo.Student;
以上所述是小编给大家介绍的SQL Server 2012 开窗函数,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
相关推荐
ItBJLan 2020-06-16
专注前端开发 2020-10-21
苏康申 2020-11-13
vitasfly 2020-11-12
oraclemch 2020-11-06
liuyang000 2020-09-25
FellowYourHeart 2020-10-05
赵继业 2020-08-17
whyname 2020-08-16
Seandba 2020-08-16
dbasunny 2020-08-16
拼命工作好好玩 2020-08-15
langyue 2020-08-15
写程序的赵童鞋 2020-08-03
Accpcjg 2020-08-02
tydldd 2020-07-30
好记忆也需烂 2020-07-28