-- SQL to remove invalid group entries from regulatory_group_documents
-- where groupcode does not exist in viamed_customer_stock_references_groupcodes with active = 0

DELETE FROM regulatory_group_documents
WHERE groupcode NOT IN (
    SELECT groupcode
    FROM viamed_customer_stock_references_groupcodes
    WHERE active = 0
);