DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`riskAllClients`(maxRiskDate DATE)
BEGIN
	
	DROP TEMPORARY TABLE IF EXISTS tmp.client_list;
    CREATE TEMPORARY TABLE tmp.client_list
    (PRIMARY KEY (Id_Cliente))
		ENGINE = MEMORY
		SELECT id Id_Cliente, null grade FROM vn.client;
    
    CALL vn2008.risk_vs_client_list(maxRiskDate);
    
	SELECT 
		c.RazonSocial,
        c.Id_Cliente,
        c.Credito,
        CAST(r.risk as DECIMAL (10,2)) risk,
        CAST(c.Credito - r.risk as DECIMAL (10,2)) Diferencia,
        c.Id_Pais
	FROM
		vn2008.Clientes c
			JOIN tmp.risk r ON r.Id_Cliente = c.Id_Cliente
			JOIN tmp.client_list ci ON c.Id_Cliente = ci.Id_Cliente
		GROUP BY c.Id_cliente;
        
	DROP TEMPORARY TABLE IF EXISTS tmp.risk;
    DROP TEMPORARY TABLE IF EXISTS tmp.client_list;
END$$
DELIMITER ;
