salix/db/routines/vn/procedures/creditInsurance_getRisk.sql

42 lines
1.1 KiB
MySQL
Raw Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
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,
2024-04-24 07:05:29 +00:00
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 ;