GROUP BY COALESCE(factura, cedula) fixes terceros (factura=NULL) collapsing into one row — each paciente now gets its own row. CASE WHEN in estado_final explicitly prioritizes success > warning > error instead of relying on alphabetical MAX which returned warning over success. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>