Find higher sum value and show id

Asked

Viewed 1,437 times

1

Talk guys, all right?

I have this table

Table EMPREGADO_PROJETO

+--------------+-------------+-------+
| rg_empregado | num_projeto | horas |  
+--------------+-------------+-------+
| 20202020     | 5           | 10    |  
+--------------+-------------+-------+
| 20202020     | 10          | 25    |  
+--------------+-------------+-------+
| 30303030     | 5           | 35    | 
+--------------+-------------+-------+
| 40404040     | 20          | 50    | 
+--------------+-------------+-------+
| 50505050     | 20          | 35    | 
+--------------+-------------+-------+

I need to show you which project has the most hours. For this I will need to join the projects with the same number, add up their hours and show what is the largest sum. The sum I’m managing to show, all that remains is to identify which is the project number that this sum belongs to.

Here’s what I’ve been able to do so far.

SELECT MAX(HORAS_TOTAIS) AS MAIOR_CARGA_HORARIA FROM
(SELECT DISTINCT NUM_PROJETO, SUM(HORAS) AS HORAS_TOTAIS FROM EMPREGADO_PROJETO
GROUP BY NUM_PROJETO) EMPREGADO_PROJETO

Raphael’s response worked, I only had to add the top 1 in the first select and it worked exactly as I wanted it.

  • thinker, don’t keep posting updates of the question here below, as if they were answers. Click [Edit] and add direct to the question, and delete the other posts.

  • okay, it’s done.

3 answers

0

I think it works that way, since it’s to catch the biggest:

 SELECT TOP 1 total.horas_totais, total.rg, total.num_projeto
    FROM ( SELECT DISTINCT NUM_PROJETO as num_projeto,
           SUM(HORAS) AS horas_totais,
           RG_EMPREGADO as rg
           FROM EMPREGADO_PROJETO
           GROUP BY NUM_PROJETO
         ) total
    ORDER total.horas_totais DESC;

Or

SELECT TOP 1 sum(horas) AS TOTAL_HORAS,
       NUM_PROJETO,
       RG_EMPREGADO
FROM  EMPREGADO_PROJETO
GROUP BY NUM_PROJETO ORDER BY TOTAL_HORAS desc;
  • Thanks Ivan. Your possible solutions did not work.

  • "My possible solutions" have not been tested, I usually use Mysql, so you must have seen a query "Frankenstein" is totally different the concept. But I tried to follow the logic of what you need, and what you asked in the question, I’ll make it clearer next time. Thank you for the experience.

  • I did test Ivan. I did not want to belittle his help, far from it. Perhaps because my knowledge is still low in this area I have tested it wrong. I’m sorry if I offended you, it was not the intention.

0

I believe this query solves your problem:

SELECT SUM(HORAS) AS HORAS, NUM_PROJETO FROM EMPREGADO_PROJETO
GROUP BY NUM_PROJETO
ORDER BY HORAS DESC

Upshot:

inserir a descrição da imagem aqui

  • Thanks for the answer, but it’s still not the way I need it.

  • What you need more/less?

  • It’s already been solved. Take a look at the end of the first post I explained.

-1


SELECT num_projeto, (SELECT DISTINCT SUM(HOURS)) AS HORAS_TOTAIS FROM EMPREGADO_PROJETO GROUP BY num_project ORDER BY HORAS_TOTAIS DESC

inserir a descrição da imagem aqui

  • Put the full answer, preferably with result.

  • Okay, I’ll do it here and show you

  • Thanks for the answer. In this case the rg_employee does not matter, only the num_project and the time charge.

  • tidy friend @thinker

  • Opa, now yes, gave Raphael Caldas straight! Thank you very much!

  • Downvoter please tell me what I did wrong.

  • I’ve voted old, but I think I’m a very young user, so I’m not telling you.

  • it wasn’t you, someone downvoted for no apparent reason

Show 3 more comments

Browser other questions tagged

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