38682-vm/db/migrations/015_add_redemptions_to_customers.sql
2026-02-23 13:12:33 +00:00

12 lines
395 B
SQL

-- Add loyalty_redemptions_count to customers table
ALTER TABLE customers ADD COLUMN loyalty_redemptions_count INT DEFAULT 0;
-- Optional: Initialize count from existing orders
UPDATE customers c
SET c.loyalty_redemptions_count = (
SELECT COUNT(*)
FROM orders o
JOIN payment_types pt ON o.payment_type_id = pt.id
WHERE o.customer_id = c.id AND pt.name = 'Loyalty Redeem'
);