My table structure is as described in this post:
name | version | processed | processing | updated | ref_time
------+---------+-----------+------------+----------+----------
abc | 1 | t | f | 27794395 | 27794160
def | 1 | t | f | 27794395 | 27793440
ghi | 1 | t | f | 27794395 | 27793440
jkl | 1 | f | f | 27794395 | 27794160
mno | 1 | t | f | 27794395 | 27793440
pqr | 1 | f | t | 27794395 | 27794160
Based on this answer, I am deriving a list of ref_time
values which I want to use as a basis for deleting 'old' entries from status_table
.
This is the query to generate the list of relevant ref_time
values:
WITH main AS
(
SELECT ref_time,
ROUND(AVG(processed::int) * 100, 1) percent
FROM status_table
GROUP BY ref_time ORDER BY ref_time DESC, percent DESC
)
SELECT ref_time FROM main WHERE percent=100 OFFSET 2;
For example this might return:
ref_time
----------
27794880
27794160
I can then use this to DELETE
all relevant entries in the status_table
:
DELETE FROM status_table
WHERE ref_time IN
(
WITH main AS
(
SELECT ref_time,
ROUND(AVG(processed::int) * 100, 1) percent
FROM status_table
GROUP BY ref_time ORDER BY ref_time DESC, percent DESC
)
SELECT ref_time FROM main WHERE percent=100 OFFSET 2
);
But I have another table named data_table
, which also has a ref_time
column, and I want to DELETE
entries from that table on the same basis, i.e. any rows having ref_time
in the above list.
How do I achieve this without duplicating the query used to generate the ref_time
list?