Skip to content

Instantly share code, notes, and snippets.

@zhu3pang
Last active June 19, 2021 08:13
Show Gist options
  • Save zhu3pang/8b90e0a9ef8e9191457f10f0382b8ab0 to your computer and use it in GitHub Desktop.
Save zhu3pang/8b90e0a9ef8e9191457f10f0382b8ab0 to your computer and use it in GitHub Desktop.
window function 「lag」and 「lead」in sql
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