Olá,
Preciso gerar um relatório que calcule a porcentagem de preventivas não concluídas de um funcionário e exiba no relatório
Para fazer isso criei uma tabela onde guardo nela as informações como nome do funcionário, porcentagem e o mês referente.
A questão é que não posso por exemplo gerar 2 informações para um mesmo funcionário em um mesmo mês, então fiz o seguinte trigger abaixo:
CREATE TRIGGER `preventiva_AUPD` AFTER UPDATE ON preventiva FOR EACH ROW
-- Edit trigger body code below this line. Do not edit lines above this one
if exists (select idFuncionario as fatoIdFun,data from fato_performance_individual where idFuncionario = NEW.idFuncionario and data = NEW.data) then
UPDATE `preventiva`.`fato_performance_individual`
INNER JOIN (SELECT preventiva.idFuncionario,nome,count(idPreventiva) * t.factor AS pct,MONTH(data) as MES,data
FROM preventiva
INNER JOIN funcionario on funcionario.idFuncionario = preventiva.idFuncionario
JOIN (SELECT 100/COUNT(*) AS factor FROM preventiva where idFuncionario = NEW.idFuncionario and data= NEW.data ) AS t
where preventiva.idFuncionario = NEW.idFuncionario and data= NEW.data and concluida = 0
GROUP BY data,concluida) as f -- Tabela para o join
ON `preventiva`.`fato_performance_individual`.idFuncionario = f.idFuncionario
SET
`porcentagem` = f.pct
WHERE `fato_performance_individual`.idFuncionario = NEW.idFuncionario and `fato_performance_individual`.data = NEW.data;
else
INSERT INTO `preventiva`.`fato_performance_individual`
(`idFuncionario`,`Nome`,`porcentagem`,`mes`,`data`)
SELECT preventiva.idFuncionario,nome,count(idPreventiva) * t.factor AS pct,MONTH(data) as MES,data
FROM preventiva
INNER JOIN funcionario on funcionario.idFuncionario = preventiva.idFuncionario
JOIN (SELECT 100/COUNT(*) AS factor FROM preventiva where idFuncionario = NEW.idFuncionario and data= NEW.data ) AS t
where preventiva.idFuncionario = NEW.idFuncionario and data= NEW.data and concluida = 0
GROUP BY data,concluida;
end if;
Minha questão é, essa é de fato uma boa solução?
É performático fazer isso ? Vou ter muito impacto negativo? Se sim porque?
Se algum poder ajudar agradeço.
Att