关键词:sql server窗口函数,窗口函数,分析函数

如果分析函数不可用,那么可能是版本还不支持

Window Function 包含了 4 个大类。分别是:

1 - Rank Function  

1.1 Rank() Over()

1.2 Row_Number() Over()

1.3 Dense_Rank() Over()

1.4 NTILE(N) Over()

2 - Aggregate Function

2.1 - Sum() Over()

2.2 - Count() Over()

2.3 - AVG() Over()

2.4 - MIN() Over()

2.5 - MAX() Over()

3 - Offset Function

3.1 Lead()

3.2 LAG()

3.3 First_Value()

3.4 Last_Value()

3.5 Nth_Value()

4 - Distribution Function.

4.1- PERCENT_RANK()

4.2 - CUME_DIST()

4.3 - PERCENT_COUNT()

4.4 - PERCENT_DISC()

1 - Rank Function 估计是平常用到最多的一类 window Function.

1.1 Rank() Over()

1.2 Row_Number() Over()

1.3 Dense_Rank() Over()

1.4 NTILE(N) Over()

使用:四大排名函数

注意点

这四个函数,要注意的地方有两点:

a. Rank() Over() 与 Row_Number() Over() :

  两者唯一的区别,就在于Row_Number() Over() 真正实现了相同条件的两条或者多条记录是用唯一值来区别的

b. Rank() Over() 与 Dense_Rank() Over() :

  这两者的区别,在于他们对位于相同排名之后的名次,是接着相同排名的连续数(Rank) 还是相隔 N 个相同记录个数之后的连续数(Dense_Rank)。

所以 Rank 出来的结果都是连续数字,而 Dense_Rank 出来的结果有可能有跳格数。

例子(更多参考四大排名函数

第一种,我们平常用 Row_Number() 加 Top (N) 来实现 :

SELECT
TOP (100) *
FROM
(
SELECT
OrderId,
OrderMonth,
OrderAmount,
Row_Number () OVER (OrderBy OrderAmount DESC) AS Amt_Order
FROM
FctSales
) tmp
WHERE
Amt_Order BETWEEN 2000
AND 3000

2 - Aggregate Function. 用于聚合数据

2.1 - Sum() Over()

2.2 - Count() Over()

2.3 - AVG() Over()

2.4 - MIN() Over()

2.5 - MAX() Over()

在使用 Aggregation 函数的时候,唯一要注意的地方就是 Order 子句。

function_name(<arguments>) Over(
[ <window partition clause>]
[ <window Order clause>
[ <window frame clause>]
])
Over::
Over(
[ <PARTITION BY clause> ]
[ <ORDER BY clause> ]
[ <ROW or RANGE clause> ]
) <window frame clause>::窗口中的窗口
ROWS | RANGE
BETWEEN
UNBOUNDED PRECDEDING |
<N> PRECEDING |
<N> FOLLOWING |
CURRENT ROW
AND
UNBOUNDED FOLLOWING |
<N> PRECEDING |
<N> FOLLOWING | CURRENT ROW 举一个例子:
--利用嵌套统计累加和
;with temp1 as (
select 1 as id ,1 as num union all
select 1 as id ,2 as num union all
select 1 as id ,3 as num union all
select 2 as id ,4 as num union all
select 2 as id ,5 as num union all
select 2 as id ,6 as num
)
select *,sum(num) over(partition by id order by num asc rows between unbounded preceding and current row) from temp1


3 - Offset Function:定位记录

3.1 Lead() --自上而下(lead lag 参考:https://blog.csdn.net/leewhoee/article/details/20264653/

3.2 LAG() --自下而上

3.3 First_Value() --第一个值

3.4 Last_Value() --最后一个值

3.5 Nth_Value() --第N个值

这一类比较好理解,根据当前的记录,获取前后 N 条数据。

举例:

  LEAD ( scalar_expression [ ,offset ] , [ default ] )     OVER ( [ partition_by_clause ] order_by_clause )

  LEAD(score,1,0) over(order by score) as next_score

  根据score排序,第1行之后开始的1行(即第2行的score列值),default对应的是,如果是末行或者值为null,则给个默认值

  

4 - Distribution Function: 分布函数

4.1- PERCENT_RANK()

4.2 - CUME_DIST()

4.3 - PERCENT_COUNT()

4.4 - PERCENT_DISC()

参考自:https://www.jianshu.com/p/bfc39dcb73f9
参考文献:https://www.cnblogs.com/CareySon/p/3411176.html

最新文章

  1. 名词含义阅读 todolist
  2. 从双十一战报看传统大家电如何O2O转型?
  3. linux基础-基本命令的讲解(1-7单元)
  4. 《JavaScript权威指南》学习笔记 第八天 Node Tree
  5. Orleans是什么 (一)
  6. BZOJ1114 : [POI2008]鲁滨逊逃生Rob
  7. 在Linux上使用的10种云备份方案
  8. V-rep学习笔记:转动关节1
  9. [AngularJS] Using Services in Angular Directives
  10. javascript函数基础
  11. Linux 常用命令记录
  12. 又一次拾起C语言的威严
  13. JSP内置对象---总结
  14. FreeHttp1.1升级说明
  15. Linux下使用http协议下载文件
  16. Nginx 优先选择连接最少的上游服务器
  17. php WNMP(Windows+Nginx+Mysql+php)配置笔记
  18. Android-WebView与本地HTML (Java调用---&gt;HTML的方法)-(new WebView(this)方式)
  19. vue+node+mongoDB前后端分离个人博客(入门向)
  20. TCP和UDP的区别?

热门文章

  1. java8中规范的四大函数式接口
  2. Codeforces 622F The Sum of the k-th Powers ( 自然数幂和、拉格朗日插值法 )
  3. no matches for kind &quot;ReplicaSet&quot; in version &quot;extensions/v1beta1&quot;
  4. 论文阅读:Camdoop: Exploiting In-network Aggregation for Big Data Applications
  5. reactjs 的 css 模块化工具 styled-components 升级后 createGlobalStyle 废除,使用 createGlobalStyle 的方案
  6. CodeForces 349B--Color the Fence(贪心)
  7. 二分mid的取法
  8. 原生Js_使用setInterval() 方法实现图片轮播功能
  9. R_Studio模拟学生成绩对数据简单分析
  10. 第七周总结&amp;实验报告5