SELECT sucursal ,sum(a + b) as venta , venta * porcentaje FROM ......
El tema esta en esta comsulta: Fijense que se repiten los case varias veces.
Código SQL:
Ver original
SELECT s, art , f , CASE WHEN dl IS NULL THEN venta ELSE dl * venta / dt END AS proymes , CASE WHEN porcentaje IS NULL THEN CASE WHEN dl IS NULL THEN venta ELSE dl * venta / dt END ELSE CASE WHEN dl IS NULL THEN venta ELSE 100 * (dl * venta / dt) / porcentaje END AS proy FROM ventas LEFT JOIN historicos AS h ON h.sucursal = s AND h.mes = strftime('%m' , f||'-01') LEFT JOIN proydias AS P ON p.sucursal = s AND strftime('%m',p.mes) = strftime('%m' , f||'-01') ;