-- Cartera con corte a una fecha

SELECT ch.id_sucursal AS Suc,ch.id_cliente AS NIT,c.nombre_cliente AS Cliente,
cs.direccion AS Direccion,z.descripcion AS Ciudad,cs.telefono AS Telefono,
SUM(ch.valor) AS Cartera,MAX(cs.cupo) AS Cupo
FROM `cartera_historico` ch
JOIN cliente c
USING (id_cliente)
JOIN cliente_has_sucursal cs
USING (id_sucursal, id_cliente)
JOIN zona z
ON (cs.ciudad = z.id_zona)
WHERE ch.fecha_historico = '2012-07-31'
GROUP BY ch.id_sucursal, ch.id_cliente
