If I have two Postgres tables, say
CREATE TABLE store (
id text,
name text
)
CREATE TABLE product (
store_id text,
id text,
created_at timestamp
)
I know I can get a table with the newest product with
SELECT product.*
FROM store JOIN (SELECT DISTINCT ON (store_id)
FROM product ORDER BY store_id, created_at DESC)
AS newest_product_by_store
ON newest_product_by_store.store_id = store.id
How, then, would I do a DELETE USING that deletes every product not in newest_product_by_store
?