Add query results with group by

0

I have the following query

SELECT count(dp.id_dieta) * 
    (select d.dieta from dietas d where d.idDietas = dp.id_dieta) as t
FROM dietaspartes dp 
WHERE dp.id_parte in 
    (SELECT p.idparte as part 
    FROM partes p 
    WHERE p.IDProyecto = 1111)
GROUP BY id_dieta

That returns two results: 50 and 30

I would like to modify this query to return the sum of these two values, but with the possibility that if they are 3 or X, return the sum of those X values.

If I enclose it with a Sum it tells me that I make an invalid use of group by

I'm a bit blocked with this, thanks for the help.

    
asked by U. Busto 03.12.2018 в 15:14
source

2 answers

3

The problem that gives you the system of the invalid use of group by is because the value of group by must be part of the select , in your case you must place

 SELECT id_dieta, 
        SUM(COUNT(dp.id_dieta) * d.dieta) AS t
    FROM dietaspartes dp
        INNER JOIN dietas d ON dp.id_dieta = d.idDietas
    WHERE dp.id_parte in (SELECT p.idparte 
                                FROM partes p 
                                WHERE p.IDProyecto = 1111)   
    GROUP BY id_dieta
    
answered by 03.12.2018 / 15:20
source
0

In the end I managed to solve it and it stays that way, maybe someone will do well

    SELECT SUM(t)
FROM
(
SELECT COUNT(dp.id_dieta) * (
SELECT d.dieta
FROM dietas d
WHERE d.idDietas = dp.id_dieta) AS t
FROM dietaspartes dp
WHERE dp.id_parte IN 
 (
SELECT p.idparte AS part
FROM partes p
WHERE p.IDProyecto = 1111)
GROUP BY id_dieta) AS tt
    
answered by 03.12.2018 в 15:44