Need to identify non contigious rows
-
28-02-2021 - |
Question
My table structure is as follows:
create table daily_summary
(
id uuid default uuid_generate_v4() not null
primary key,
machine bigint not null
odometer_start numeric(10, 3) not null,
odometer_end numeric(10, 3) not null,
distance numeric(10, 3) not null,
date timestamp not null,
...
);
The data in the daily_summary table should be contigious for a machine, ie the odometer_end for a row for a machine should be the odometer_start for the same machine in the next row when ordered by date.
Is there a query using lead() or any other way that I can use to identify non contigious row?
Solution
Something like this:
select *
from (
select ds.*,
lag(ds.odometer_end) over (partition by ds.machine order by ds."date") as prev_end
from daily_summary ds
) t
where odometer_start <> prev_end + 1
order by "date"
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange