-- schema version 30 to 31

PRAGMA writable_schema = ON;

DROP VIEW v_under_delivery_bucket_strict;
CREATE VIEW v_under_delivery_bucket_strict AS
SELECT c.year, c.mgnr, c.bucket, c.min_kg, SUM(COALESCE(p.weight, 0)) AS weight
FROM v_area_commitment_bucket_strict c
    LEFT JOIN v_payment_bucket_strict p ON (p.year, p.mgnr, p.bucket) = (c.year, c.mgnr, c.bucket) OR (p.year, p.mgnr, p.bucket) = (c.year, c.mgnr, c.bucket || '_')
GROUP BY c.year, c.mgnr, c.bucket
ORDER BY c.year, c.mgnr, c.bucket;

PRAGMA schema_version = 3001;
PRAGMA writable_schema = OFF;
