2
Good evening, everyone.
I have a little problem that I’ve been trying to solve for a while and I came to ask here.
It is as follows: I have three important entities for this solution: To make it easier, I’ll just put the "minimum fields".
- Funcionario (cod_func, name, cod_cargo)
- Position (cod_cargo, Description)
- History (cod_position, cod_func, dataInicio, dataFim)
My system is as follows. Employees always face job changes. And when this change occurs, the positions are stored in a history table with the position, the employee and the date.
In other words, you have a relationship where an employee has a position. A position has several employees.
The historical table has two foreign keys.
Now I want to select, for each position, the employees who MORE had change of position.
That is, for each position, the employee who has most changed position and is in office now. Understand?
I tried to do it this way, but I don’t know "reduce more".
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
Oops. Thank you. Upstairs you have the fields. Or you want the code to create?
– user17245
It does not need creation no. In fact, if I understood correctly, the more entries in the history, the more changes in office, would that be? Have some DBMS preferably?
– Bacco
That’s right. SQL Server. But helping me in logic on any DBMS is great.
– user17245
I also believe that it works without a BD to test
– Iara Abreu do Nascimento