I have a table:
CREATE TABLE t_table (
c_id int4 NOT NULL,
c_date_called int4 NOT NULL,
CONSTRAINT t_table_un UNIQUE (c_id, c_date_called)
);
that stores distinct snapshots of data, with data as such:
INSERT INTO t_table (c_id, c_date_called)
VALUES
(1,9),
(2,9),
(3,9),
(4,9),
(5,9),
(1,12),
(2,12),
(3,12),
(5,12),
(1,17),
(3,17)
;
Against this table I can run an anti-join, comparing the current
snapshot and one previous
snapshot:
--EXPLAIN ANALYSE VERBOSE
SELECT prev.*
FROM t_table AS prev
LEFT JOIN t_table AS cur ON (prev.c_id = cur.c_id) AND cur.c_date_called = 17
WHERE prev.c_date_called = 9
AND cur.c_id IS NULL
;
returns the data I expect when finding the IDs not present in the current c_date_called
:
c_id|c_date_called|
----+-------------+
2| 9|
4| 9|
But how do I apply the anti-join across multiple distinct c_date_called
collecting the results and merging them as compared against the current
c_date_called
?
Well. Window functions with anti-joins.. yeah need help.
2
Answers
Well to report the complete state of the
is
s in the snapshots, i.e. a snapshot with a newid
not present in the previous snapshot resp. theid
removed, i.e. not present in the next snapshot, you do not need an anti-join. Which you do not use in your example anyway.First define the sequence of the snapshots using integers and flag the current snapshot
Than join this supporting information to your main table and add two attribuites based on the
lag
andlead
window function of the snapshot index that identify if the previous / next snapshot with the givenid
is consecutive or if there is a gap. The logic should be pretty self-explained.Note that I added
id
6 that was introduced in the second snapshot to demonstrated this used case.I read this problem as:
I have a table t_table(c_id, c_date_called).
In this we have sets of data (c_id) per snapshot date (c_date_called).
The current snapshot which is the snapshot where c_date_called is highest.
Please give me an overview of all previous data (c_id) which was present in previous snapshots, but not in the current snapshot.
If so, this can be done simply without window functions:
Which gives the result: