List in each position, the employees who most had change of position

Asked

Viewed 2,053 times

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?

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

  • That’s right. SQL Server. But helping me in logic on any DBMS is great.

  • I also believe that it works without a BD to test

2 answers

1

I think it works , without a BD to test

Funcionario (cod_func, nome, cod_cargo)
Cargo (cod_cargo, descricao)
Historico (cod_cargo, cod_func, dataInicio, dataFim)

--history of employees

select cod_cargo,cod_func,sum(*) qtd_cargos
from   historico
group by cod_cargo,cod_func

--"champions"

select cod_cargo,cod_func
from (select cod_cargo,cod_func,sum(*) qtd_cargos
      from   historico
      group by cod_cargo,cod_func) virtual1
where qtd_cargos = (select max(qtd_cargos)
                    from (select cod_cargo,cod_func,sum(*) qtd_cargos
                          from   historico
                          group by cod_cargo,cod_func) virtual2
                    where virtual2.cod_cargo = virtual1.cod_cargo)

Maybe a solution via "analityc functions" will be cleaner but I believe that this will work in all DBMS that accepts virtual tables.

0

This answer goes to SQL Server. If you need it for another database, please indicate in the question.

SELECT c.descricao, f.nome, h.qtd
FROM (
    SELECT cod_func, qtd = count(1) over (partition by cod_func)
    from historicos
    group by cod_func
    order by qtd desc
) h
JOIN funcionario f on f.cod_func = h.cod_func
JOIN cargo c on c.cod_cargo = f.cod_cargo
  • Thanks. But it still doesn’t work. The way you did, you’re just ordering. What I want is to pick up is for each position the employee who had the most transfer. That is, if you, Gypsy has already been through 5 positions and works as a Manager. So it should be listed like this: Gypsy Manager and so on, for each position. Thanks for the help.

  • There’s no point in saying that. What’s the problem?

  • You had accidentally pressed enter. For example, you went through 5 positions and João went through 4 and is also a manager. So for the manager should be shown you because you have more job changes. And so for each job.

  • I get it. I’m modifying the answer.

  • Oops. Error. Msg 8120, Level 16, State 1, Line 1 Column 'h. Qtd' is invalid in the select list because it is not contained in either an Aggregate Function or the GROUP BY clause.

  • Executing the subquery only returns what?

  • Subquery returns the amount for each player

  • I added in group by bass group by h.Qtd, but then he repeats the teams.

  • @user17245 Truth. The GROUP BY you don’t need it. I forgot to take it. Note that I made an edit. What the output looks like after that?

Show 4 more comments

Browser other questions tagged

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