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

36 lines
744 B
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`vn-admin`@`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,
2024-04-24 07:05:29 +00:00
co.name 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 ;