PostgreSQL 的 lag函数
create table test(id integer);
insert into test values(1);
insert into test values(3);
insert into test values(6);
insert into test values(10);
insert into test values(15);
insert into test values(24);
------------------------------------------
select id, lag(id, 1,0) from test;
id lag
1 0
3 1
6 3
10 6
15 10
24 15
------------------------------------------
select id, lag(id, 2,0) from test;
id lag
1 0
3 0
6 1
10 3
15 6
24 10
------------------------------------------
select id, lag(id, 1,1) from test;
id lag
1 1
3 1
6 3
10 6
15 10
24 15
------------------------------------------
select id, lag(id, 2,1) from test;
id lag
1 1
3 1
6 1
10 3
15 6
24 10