Good evening guys.
I have a problem that I have been trying to solve for some time and I came to ask you here.
It is as follows: I have three important entities for this solution: For ease, I'll just put the "minimum" fields.
- Official (cod_func, name, cod_cargo)
- Charge (cod_chargo, description)
- History (cod_cargo, cod_func, startDate, dataFim)
My system is as follows. Employees always face change of charge. And when that change occurs, jobs are stored in a history table with the job title, employee, and date.
That is, there is a relationship in which an employee has a charge. One job has several employees.
The historical table has two foreign keys.
Now I want to select, for each position, the employees that had the most change of position.
That is, for each position, the employee who has changed the most and is in the position now. Do you understand?
I tried to do it this way, but I do not know how to "reduce it further."
SELECT c.descricao, f.nome, count(*) as qtd
FROM historicos
JOIN funcionario f on f.cod_func = historicos.cod_func
JOIN cargo c on c.cod_cargo = f.cod_cargo
GROUP BY c.dsc_cargo, f.nome