postgres=# create table demo(gruppe int, wert int);
CREATE TABLE
postgres=# insert into demo values (1, 10);
INSERT 0 1
postgres=# insert into demo values (1, 11);
INSERT 0 1
postgres=# insert into demo values (1, 20);
INSERT 0 1
postgres=# insert into demo values (2, 20);
INSERT 0 1
postgres=# insert into demo values (2, 30);
INSERT 0 1
postgres=# insert into demo values (3, 1);
INSERT 0 1
postgres=# insert into demo values (3, 2);
INSERT 0 1
postgres=# insert into demo values (3, 3);
INSERT 0 1
postgres=# insert into demo values (3, 6);
INSERT 0 1
postgres=# select gruppe, wert, wert - lag(wert) over (partition by gruppe order by wert) as differenz from demo;
gruppe | wert | differenz
--------+------+-----------
1 | 10 |
1 | 11 | 1
1 | 20 | 9
2 | 20 |
2 | 30 | 10
3 | 1 |
3 | 2 | 1
3 | 3 | 1
3 | 6 | 3
(9 rows)
postgres=# with tmp as (select gruppe, wert, wert - lag(wert) over (partition by gruppe order by wert) as differenz from demo) select * from tmp where differenz is null or differenz > 5;
gruppe | wert | differenz
--------+------+-----------
1 | 10 |
1 | 20 | 9
2 | 20 |
2 | 30 | 10
3 | 1 |
(5 rows)
postgres=#