0
Good afternoon. I need to make a list of employees per year. For example. Active employees 2018, However I’m having problems, because in 2019 some were fired there not list the same, and use the table Employees and Examesfuncionarios.
Edited: Next, I used the check to know if I had exams resigned, so far so good, I did not list this person, but that same person has in the same year the exam return to work, or even another exam and I do not want and I can not display it. How do I make the condition that if there is exam resigned for such person it jumps the person even if it has other exams.
Follow the Codes and tables.
Employee Table ->
CREATE TABLE `funcionario` (
`CodFuncionario` int(11) NOT NULL AUTO_INCREMENT,
`funcionario_CodEmpresa` int(11) NOT NULL,
`funcionario_CodSetor` int(11) DEFAULT NULL,
`funcionario_CodCBO` varchar(6) DEFAULT NULL,
`funcionario_Nome` varchar(50) DEFAULT NULL,
`funcionario_DataNac` date DEFAULT NULL,
`funcionario_DataAdm` date DEFAULT NULL,
`funcionario_Sexo` varchar(1) DEFAULT NULL,
`funcionario_CTPS` varchar(20) DEFAULT NULL,
`funcionario_RG` varchar(10) DEFAULT NULL,
`funcionario_NIT` varchar(12) DEFAULT NULL,
`funcionario_Ativo` varchar(1) DEFAULT NULL,
`funcionario_Cpf` varchar(11) DEFAULT NULL,
`funcionario_DataDemissao` date DEFAULT NULL,
`funcionario_TrabalhoAltura` varchar(1) DEFAULT NULL,
`funcionario_TrabalhoConfinado` varchar(1) DEFAULT NULL,
`funcionario_HoraEntrada` varchar(100) DEFAULT NULL,
`funcionario_HoraSaida` varchar(100) DEFAULT NULL,
`funcionario_InicioRepouso` varchar(100) DEFAULT NULL,
`funcionario_FimRepouso` varchar(100) DEFAULT NULL,
`funcionario_Epi` longtext DEFAULT NULL,
`funcionario_RiscoFisico` longtext DEFAULT NULL,
`funcionario_RiscoQuimico` longtext DEFAULT NULL,
`funcionario_RiscoBiologico` longtext DEFAULT NULL,
`funcionario_RiscoErgonomico` longtext DEFAULT NULL,
`funcionario_HoraEntrada_S` varchar(100) DEFAULT NULL,
`funcionario_HoraSaida_S` varchar(100) DEFAULT NULL,
`funcionario_InicioRepouso_S` varchar(100) DEFAULT NULL,
`funcionario_FimRepouso_S` varchar(100) DEFAULT NULL,
`funcionario_ExameOcupacional` longtext DEFAULT NULL,
PRIMARY KEY (`CodFuncionario`) USING BTREE
)
Tebela Examesfuncionarios ->
CREATE TABLE `examefuncionario` (
`idExameFunc` int(11) NOT NULL AUTO_INCREMENT,
`exameFunc_CodFuncionario` int(11) NOT NULL,
`exameFunc_NrSeq` int(11) NOT NULL,
`exameFunc_CodExame` int(11) DEFAULT NULL,
`exameFunc_Data` date DEFAULT NULL,
`exameFunc_Tipo` varchar(1) DEFAULT NULL,
`exameFunc_Natureza` varchar(1) DEFAULT NULL,
`exameFunc_Apto` varchar(1) DEFAULT NULL,
`exameFunc_DataProximo` date DEFAULT NULL,
`exameFunc_Resultado` varchar(1) DEFAULT NULL,
`exameFunc_FlgRefSeq` varchar(1) DEFAULT NULL,
`exameFunc_DataUltimo` date DEFAULT NULL,
PRIMARY KEY (`idExameFunc`) USING BTREE
)
Mysql codes 1st form ->
select funcionario.CodFuncionario,funcionario.funcionario_Nome
from funcionario
join examefuncionario on examefuncionario.exameFunc_CodFuncionario = funcionario.CodFuncionario and date_format(examefuncionario.exameFunc_Data, "%Y") = '2018'
where funcionario_CodEmpresa = 179
and date_format(funcionario_DataAdm, "%Y") <= '2018'
group by funcionario.CodFuncionario
order by funcionario.funcionario_Nome ASC
Mysql codes 2nd form ->
select funcionario.CodFuncionario,funcionario.funcionario_Nome
from funcionario
join examefuncionario on examefuncionario.exameFunc_CodFuncionario = funcionario.CodFuncionario and date_format(examefuncionario.exameFunc_Data, "%Y") = '2018'
where funcionario_CodEmpresa = 179
and date_format(funcionario_DataAdm, "%Y") <= '2018'
and funcionario_Ativo = 'S'
group by funcionario.CodFuncionario
order by funcionario.funcionario_Nome ASC
Edited code:
select *
from funcionario
join examefuncionario
on examefuncionario.exameFunc_CodFuncionario = funcionario.CodFuncionario
and date_format(examefuncionario.exameFunc_Data, "%Y") = '2018'
and examefuncionario.exameFunc_Natureza != 'D'
where funcionario_CodEmpresa = 179
and date_format(funcionario_DataAdm, "%Y") <= '2018'
group by funcionario.CodFuncionario
order by funcionario.funcionario_Nome ASC
I need you to list the employees who didn’t have their exams in 2018. In 2018 the employee was active but in 2019 is inactive, so when I use the Where of the same some who in 2018 were active not list.
It’s not enough that you list the ones that are working with NULL (not fired) or the year of function_DataWill be greater than or equal to 2018 (were active at least sometime in 2018)?
– anonimo
The way your join criteria is you will only select at the join employees who have taken at least one exam in 2018. It does not seem to me to agree with the text of your question.
– anonimo
As your table examefuncnario has the field examFunc_Data it is not necessary to also check if the year of such date is 2018 and the type of exam is what you want?
– anonimo
I need to verify, because this report is annual, IE, in the year 2019 everything changes.
– José Luis
But the codes you posted do not make such a check.
– anonimo
date_format(examandfeature.exameFunc_Data, "%Y") = '2018' only displays the exams for 2018. But there are some employees who do two, one of them is fired and I can not list it, and also I can not use the function_Ativo = ’S' because then will not list others that in 2018 was active but now in 2019 are inactive.
– José Luis
What is your version of
MySQL
?– Sorack