Files
4ventures-client-portal/sql/leads_status_timing.sql
4Ventures cf27eba74d feat: KPIs con totales absolutos, backfill ads y ventas en cruce canal
Comparativas WoW/MoM/YoY muestran prev/Δ además del %; sync ads admite
histórico desde 2025; tabla ads↔CRM añade cierres, ventas y ROAS.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 10:23:36 +02:00

23 lines
893 B
SQL

-- CRM date correctness: preserve lead birth vs status change timing.
-- Run in Supabase SQL editor (service role / dashboard).
alter table public.leads
add column if not exists updated_at timestamptz;
alter table public.leads
add column if not exists status_changed_at timestamptz;
comment on column public.leads.created_at is
'When the contact entered Clientify / first seen (alta).';
comment on column public.leads.updated_at is
'Last webhook/sync touch.';
comment on column public.leads.status_changed_at is
'When status last changed (e.g. → cliente). Use this for “ventas en el periodo”.';
-- Backfill: unknown status timing → treat as created_at (best effort).
update public.leads
set
updated_at = coalesce(updated_at, created_at, now()),
status_changed_at = coalesce(status_changed_at, created_at, now())
where updated_at is null or status_changed_at is null;