Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

窗口函数的操作 #73

Open
astak16 opened this issue Sep 23, 2023 · 0 comments
Open

窗口函数的操作 #73

astak16 opened this issue Sep 23, 2023 · 0 comments

Comments

@astak16
Copy link
Owner

astak16 commented Sep 23, 2023

窗口函数的操作

[你要的操作] 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

rowsinterval 表示行数偏移量,和 between 没啥区别,都表示前 n

rangeinterval 表示时间段的间距,需要指定单位,比如 daymonthyear

-- 如果时间不连续,就能看出它们的区别了
rows between 5 preceding and current row 当前行和前 5 行,共 6 行数据
range between interval 5 day preceding and current row 当前行的天数和前5天,共 6 天数据
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant