I would largely simplify with an EXISTS
semi-join.
Delete only offending rows
SELECT * FROM tbl t
-- DELETE FROM tbl t
WHERE indicator <> 0
AND EXISTS (
SELECT 1
FROM tbl t1
WHERE day IN (t.day, t.day + 1)
AND t1.company = t.company
AND t1.value IS NULL
)
Using column name day
instead of date
, cause I never use basic type names as identifiers.
day + 1
works if day
is of data type date
(as it should be).
Delete the whole company
the company shall be excluded from the dataset entirely.
DELETE FROM tbl t
USING (
SELECT DISTINCT company
FROM tbl t
WHERE indicator <> 0
AND EXISTS (
SELECT 1
FROM tbl t1
WHERE day IN (t.day, t.day + 1)
AND t1.company = t.company
AND t1.value IS NULL
)
) del
WHERE t.company = del.company