Counting supervisors in pgAdmin

Asked

Viewed 29 times

0

How can I list the name of each supervisor with the amount of supervised.

select f.NomeFunc, s.ID_Superv 
from Funcionario f 
inner join Funcionario s
on s.ID_Superv = f.ID_Func
having COUNT(f.NomeFunc);

I’m trying like this but you’re making a mistake.

  • If this answer solved your problem and there is no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

1 answer

1

To get the number of employees connected to a supervisor you can use the following code. Using Count to count the number of employees in select and group by supervisor name.

SELECT f.NomeFunc, count(s.ID_Superv)
  FROM  Funcionario f 
    INNER JOIN Funcionario s ON s.ID_Superv = f.ID_Func
GROUP BY f.NomeFunc
ORDER BY f.NomeFunc

having can be used when you want to filter the Count result value. Example, if you want to check all supervisors who have more than two employees connected to it you can use the following code:

SELECT f.NomeFunc, count(s.ID_Superv)
  FROM  Funcionario f 
    INNER JOIN Funcionario s ON s.ID_Superv = f.ID_Func
GROUP BY f.NomeFunc
HAVING(count(s.ID_Superv) > 2)
ORDER BY f.NomeFunc;
  • It worked. Thank you

Browser other questions tagged

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