DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`creditInsurance_getRisk`() BEGIN /** * Devuelve el riesgo de los clientes que estan asegurados */ CREATE OR REPLACE TEMPORARY TABLE tmp.clientGetDebt (PRIMARY KEY (clientFk)) ENGINE = MEMORY SELECT * FROM ( SELECT cc.client clientFk, ci.grade FROM creditClassification cc JOIN creditInsurance ci ON cc.id = ci.creditClassificationFk WHERE dateEnd IS NULL ORDER BY ci.creationDate DESC LIMIT 10000000000000000000) t1 GROUP BY clientFk; CALL client_getDebt(util.VN_CURDATE()); SELECT c.id, c.name, c.credit clientCredit, c.creditInsurance solunion, CAST(r.risk AS DECIMAL(10,0)) risk, CAST(c.creditInsurance - r.risk AS DECIMAL(10,0)) riskAlive, cac.invoiced billedAnnually, c.dueDay, cgd.grade, c2.name country FROM tmp.clientGetDebt cgd LEFT JOIN tmp.risk r ON r.clientFk = cgd.clientFk JOIN client c ON c.id = cgd.clientFk JOIN bs.clientAnnualConsumption cac ON c.id = cac.clientFk JOIN country c2 ON c2.id = c.countryFk GROUP BY c.id; DROP TEMPORARY TABLE tmp.risk, tmp.clientGetDebt; END$$ DELIMITER ;