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

36 lines
735 B
MySQL
Raw Normal View History

DELIMITER $$
2024-04-11 07:29:25 +00:00
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`client_getRisk`(
2024-04-10 12:23:24 +00:00
vDate DATE
)
BEGIN
2024-04-10 12:23:24 +00:00
/**
* Retorna el riesgo de los clientes activos.
*
* @param vDate Fecha a calcular
*/
CREATE OR REPLACE TEMPORARY TABLE tmp.clientGetDebt
(PRIMARY KEY (clientFk))
ENGINE = MEMORY
SELECT id clientFk
FROM client
WHERE isActive;
2024-04-10 12:23:24 +00:00
CALL client_getDebt(vDate);
SELECT c.socialName,
r.clientFk,
c.credit,
CAST(r.risk AS DECIMAL (10,2)) risk,
CAST(c.credit - r.risk AS DECIMAL (10,2)) difference,
co.country
FROM client c
JOIN tmp.risk r ON r.clientFk = c.id
JOIN country co ON co.id = c.countryFk
GROUP BY c.id;
DROP TEMPORARY TABLE
tmp.risk,
tmp.clientGetDebt;
END$$
DELIMITER ;