窗口函数的操作
Opened this issue · 0 comments
astak16 commented
窗口函数的操作
[你要的操作] over(partition by <用分组的列名> order by <用于排序的列名> rows/range <窗口滑动的数据范围>)
窗口滑动的数据范围:
rows - 行号为基准,全局
range - 以 order by 为基准,组内
n preceding - 前 n 行
n following - 后 n 行
current row - 当前行
unbounded preceding - 窗口第一行
unbounded following - 窗口最后一行
interval - 偏移量,rows 和 range 代表的意思不一样
举例
-- 前 n 行到当前行 / 当前行到后 n 行 / 前 n 行到后 n 行(包括当前行) | 省略当前行
共6行: 取当前行和前五行:rows between 5 preceding and current row | rows 5 preceding
共6行: 取当前行和后五行:rows between current row and 5 following | rows 5 following
共11行:取前五行和后五行:rows between 5 preceding and 5 following |
tips:使用
range
时需要注意,如果排序的时间,不能使用2020-01-01
这种格式,需要用to_days
转换下时间order by to_days(visited_on) range between 6 preceding and current row
interval
rows
中 interval
表示行数偏移量,和 between
没啥区别,都表示前 n
行
range
中 interval
表示时间段的间距,需要指定单位,比如 day
、month
、year
等
-- 如果时间不连续,就能看出它们的区别了
rows between 5 preceding and current row 当前行和前 5 行,共 6 行数据
range between interval 5 day preceding and current row 当前行的天数和前5天,共 6 天数据