автор |
---|
Или last_value с партиционированием по is null. |
можно еще так как-то
drop table if exists tbl;
create table tbl (
id int not null,
event_id int not null,
lat int not null,
lng int not null,
constraint pk_tbl primary key(id)
);
insert into tbl(id, event_id, lat, lng) values
(70, 5, 0, 0),
(71, 5, 55, 66),
(73, 5, 66, 77),
(74, 5, 0, 0),
(75, 1, 0, 0),
(76, 6, 0, 0),
(77, 8, 50, 40),
(78, 8, 50, 39),
(79, 8, 51, 39),
(88, 5, 51, 39),
(89, 5, 0, 0),
(90, 1, 0, 0),
(91, 6, 0, 0),
(92, 5, 0, 0);
create function foo(int, int) returns int
as 'select case when $2 != 0 then $2 else $1 end'
language sql;
create aggregate omg(int)
(
sfunc = foo,
stype = int,
initcond = 0
);
select id, event_id, lat, lng,
omg(lat) over(order by id) lat1,
omg(lng) over(order by id) lng1,
omg(lat) over(order by id desc) lat2,
omg(lng) over(order by id desc) lng2
from tbl order by id;