List only if the other table does not have a dismissal

Asked

Viewed 75 times

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)?

  • 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.

  • 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?

  • I need to verify, because this report is annual, IE, in the year 2019 everything changes.

  • But the codes you posted do not make such a check.

  • 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.

  • What is your version of MySQL?

Show 2 more comments

2 answers

1


Use the NOT EXISTS as follows:

SELECT f.CodFuncionario,
       f.funcionario_Nome
  FROM funcionario f
 WHERE f.funcionario_CodEmpresa = 179
   AND EXISTS(SELECT 1
                FROM examefuncionario ef
               WHERE ef.exameFunc_CodFuncionario = f.CodFuncionario
                 AND DATE_FORMAT(ef.exameFunc_Data, "%Y") = '2018'
                 AND ef.exameFunc_Natureza <> 'D')
   AND NOT EXISTS(SELECT 1
                    FROM examefuncionario ef
                   WHERE ef.exameFunc_CodFuncionario = f.CodFuncionario
                     AND DATE_FORMAT(ef.exameFunc_Data, "%Y") = '2018'
                     AND ef.exameFunc_Natureza = 'D')
 GROUP BY f.CodFuncionario
 ORDER BY f.funcionario_Nome ASC
  • Blz, I’ll try. Thank you.

  • The point is I can’t use the condition whether he’s active or not, because that would be this instant, and I need you to list the case last year, where the employee was admitted, but this year he’s fired.

  • @Joséluis understood, I will edit the answer

  • If I solve this I’m grateful to everyone, because it’s giving me a job and I can’t do my other systems.

  • @Joséluis ready, I added in the reply. Only that does not understand people who have been rehired, I do not know if this is the case

  • Cara ta almost right, but something is missing. The right one should list 48 and is listing 44

  • The condition has to ignore anyone who has resigned, even if the same employee has two exams.

  • @Joséluis but that’s what you’re doing. See if you can’t come up with an example on DB Fiddle otherwise it becomes difficult to help you

  • Worse than it has more than 810 records in total. Hence it is difficult to separate.

  • Email me your contact. And if you can help me by remote access. [email protected]

  • What I need roughly would be so -> if such officer has a dismissal not list.

  • @Joséluis then, the problem is that query already does. Apparently you have some particularity in your tables that prevents the result from being favorable. We can only correct if you come up with an executable example. For this you do not need all the data, only the ones that are necessary for the elaboration of the answer, preferably in an executable fiddle, so you can demonstrate the problem and we can propose a solution

  • You can’t help me remotely? So I could explain it better too. This is the last process I need to deliver this system, and the client is in my ear if you know what I mean.

  • Good evening, I couldn’t use DB Fiddle or any other, so I decided to upar on Mega. https://mega.nz/#! tY0n1SLY! oIOgvNDWLRGWikDbIztzQv8Lg9TxpOFMIQBxBMDtNR0

Show 10 more comments

0

I believe you have to also put conditions related to the type or nature of examination. Just looking at the samples of the tables you passed, I get a little bit in doubt of what it would be:

exameFunc_Tipo     varchar(1) DEFAULT NULL,
exameFunc_Natureza varchar(1) DEFAULT NULL,

It might solve your problem if you put a NOT IN condition to catch employees who have not had type/nature examinations resigned in the year 2018, you understand?

There is another question... when doing the Join with the Table Examsfunctions, you already take into consideration that every employee active in 2018 did some kind of exam. You have to assess if this condition is true. Every year every employee of the company goes through some kind of examination every year? Because if you don’t pass, I imagine there will be no record in the table of employee exams... if there is no record, when you do Join, those employees who have not taken any exam in 2018 will automatically be out of the query result.

  • This, this is an annual report of everything that happens within the company. Nature would be 'Demissional, Periodical and such'. How do I use not in?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.