Hi I have database like:
id | lownumber | highnumber | content --------------------------------------- 1 | 10 | 13 | text 2 | 14 | 19 | book 3 | 6 | 9 | table ...
I want to check the lownumber and highnumber contine or not. I mean the previous highnumber + 1 equal the next line lownumber or not? how to do that in postgresql?
Advertisement
Answer
You can get the exceptions using lag():
select t.*
from (select t.*, lag(highnumber) over (order by id) as prev_highnumber
from t
) t
where lownumber <> prev_highnumber + 1;
Note: “previous” is ambiguous. I don’t know from the question if it refers to the previous row based on id or lownumber. If the latter, then change the order by.