Jeanbf
Jeanbf

Reputation: 317

How can I sum a result in sql when the other results of other column are the same?

Hi and sorry if I don't do the question the correct way

I have this query :

SELECT
t4.tar_nombre as nombre_tarea,
t2.rdi_fechacti as fecha_actividad,
t3.rea_hrstarea as horas_trabajadas

FROM
    act_usuario t1
INNER JOIN
    act_regisdiario t2
ON
    (t2.usu_id = t1.usu_id)
INNER JOIN
    act_registtarea t3
ON
    (t3.rdi_id = t2.rdi_id)
INNER JOIN
    act_tarea t4
ON
    (t4.tar_id = t3.tar_id)
WHERE
    t4.usu_id = 4 
GROUP BY

    t3.rea_id

ORDER BY
    t2.rdi_fechacti

And the query print this :

enter image description here

So, I need when in "fecha_Actividad" exist the same "nombre_actividad" , thats only show me one "fecha_actividad" and the sum of "horas_trabajadas".

For example the query show this:

enter image description here

But I need this (because the same "nombre_tarea" is in the same date):

enter image description here

The reference:

enter image description here

Sorry my english.

Upvotes: 0

Views: 23

Answers (1)

Elon Than
Elon Than

Reputation: 9765

You can simply use SUM(field) syntax after grouping.

SELECT
    t4.tar_nombre as nombre_tarea,
    t2.rdi_fechacti as fecha_actividad,
    t3.rea_hrstarea as horas_trabajadas,
    SUM(t3.rea_hrstarea)
FROM
    act_usuario t1
INNER JOIN
    act_regisdiario t2
ON
    (t2.usu_id = t1.usu_id)
INNER JOIN
    act_registtarea t3
ON
(t3.rdi_id = t2.rdi_id)
INNER JOIN
    act_tarea t4
ON
    (t4.tar_id = t3.tar_id)
WHERE
    t4.usu_id = 4 
GROUP BY
    t4.tar_nombre
ORDER BY
    t2.rdi_fechacti

There are also other functions you can use on grouped results. You can find them here.

Upvotes: 1

Related Questions