Last active
June 19, 2021 08:13
-
-
Save zhu3pang/8b90e0a9ef8e9191457f10f0382b8ab0 to your computer and use it in GitHub Desktop.
window function 「lag」and 「lead」in sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select | |
id, | |
if(id%2=0, | |
lag(student, 1) over(order by id), -- 如果id是偶数,取上一行「lag」 | |
lead(student, 1, student) over(order by id) -- 如果id是奇数,取下一行,没有下一行时取自身「lead(field, 1, default)」 | |
) as student | |
from | |
seat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment