You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
select
id, month, salary,
sum(salary) over(partition by id
order by month
range unbounded preceding
) w_salary
from employee
组合使用: between 2 preceding and 2 following
当前行上两行,当前行下两行,以及当前行的 salary 累加
select
id, month, salary,
sum(salary) over(partition by id
order by month
range between 2 preceding and2 following
) w_salary
from employee
rows 和 range 的区别
rows 是每一行在表中的实际位置,可以脱离 order by 运行
看下图中 w_salary 是将当前行和下面两行的 salary 进行计算。
不过这里 id = 1, month = 7, 8 没有和 id = 2, month = 1 的 salary 进行累加,是因为这里使用 partition by id 按 id 进行分组
select
id, month, salary,
sum(salary) over(partition by id
order by month
rows between 0 preceding and2 following
) w_salary
from employee
range 要配合 order by 使用,如果 order by <column_name> 的 column_name 不连续,在计算时也不会跳过。
看下面图中, id = 1, month = 3, 4 , w_salary 为 100, 60 ,因为没有 month = 5 ,所以它不会去计算 month = 7 。
select
id, month, salary,
sum(salary) over(partition by id
order by month
range between 0 preceding and2 following
) w_salary
from employee
这中情况下 range 和 rows 没有区别:
当前行到下边界,上 0 行到下边界值都是一样
rows between 0 preceding and unbounded following
range between 0 preceding and unbounded following
rows between current row and unbounded following
range between current row and unbounded following
上边界到当前行,上编辑到下 0 行值都是一样
rows between unbounded preceding and0 following
range between unbounded preceding and0 following
rows between unbounded preceding and current row
range between unbounded preceding and current row
The text was updated successfully, but these errors were encountered:
窗口函数
基础
窗口函数的作用是用来简化排名问题,否则将会写一大堆晦涩难懂的代码。
窗口函数与聚合函数的区别是:
排名函数:
rank()
、row_number()
、dense_rank()
,他们的用法可以看分布函数:
percent_rank()
、cume_dist()
偏移量函数:
lead()
、lag()
一些其他函数:
first_value()
、last_value()
、nth_value()
、ntile()
这些函数的详细介在这里:各种窗口函数
聚合函数也可以配合窗口函数使用,起到“累加/累计”的作用,截止到当前行的
和/最大值/最小值/平均值/个数
:聚合函数和专用函数有一个显著的区别:聚合函数需要指定列名,专用函数不需要指定列名。
滑动窗口
语法:
rows n preceding between n following
,precding
在前,following
在后,否则会报错,current row
前后都可以rows
:行号为基准range
:以order by
为基准n preceding
: 前n
行n following
:后n
行current row
:当前行unbounded preceding
:窗口第一行unbounded following
:窗口最后一行窗口区间
n preceding
当前行的前n
行当
n = 1
salary
计算salary
计算n following
当前行的后n
行,和preceding
效果一样如果单独使用
n following
会报语法错误current row
当前行,只计算当前行,和w_salary
和salary
一样unbounded preceding
窗口第一行当前行到第一行所有的
salar
计算unbounded following
:窗口最后一行,和unbounded preceding
效果一样如果单独使用会报错
between 2 preceding and 2 following
当前行上两行,当前行下两行,以及当前行的
salary
累加rows
和range
的区别rows
是每一行在表中的实际位置,可以脱离order by
运行看下图中
w_salary
是将当前行和下面两行的salary
进行计算。不过这里
id = 1, month = 7, 8
没有和id = 2, month = 1
的salary
进行累加,是因为这里使用partition by id
按id
进行分组range
要配合order by
使用,如果order by <column_name>
的column_name
不连续,在计算时也不会跳过。看下面图中,
id = 1, month = 3, 4
,w_salary
为100, 60
,因为没有month = 5
,所以它不会去计算month = 7
。range
和rows
没有区别:0
行到下边界值都是一样0
行值都是一样The text was updated successfully, but these errors were encountered: