Why is the result added twice when using SUM () with JOIN?

2

Hi, I have the following question and it is that the sum is doubled and I do not know why, it adds twice the result when using SUM() with JOIN in Mysql. Each result comes out double in INVERSION_VALOR comes out 492 instead of 246, in INVERSION_VALOR it comes out 20246 instead of 10123

SELECT I.FECHA      AS INV_FECHA,
       SUM(I.VALOR) AS INVERSION_VALOR, 
       SUM(G.VALOR) AS GASTO_VALOR 
       FROM INVERSION AS I 
       INNER JOIN GASTOS AS G ON I.FECHA = G.FECHA 
       GROUP BY(I.FECHA)'
    
asked by jhon sanchez 05.11.2018 в 23:15
source

2 answers

4

From what you say, you need to add the rows of the two tables by date. For this, you should first group each table by date independently and then join them by a join . If you do the join directly, you will duplicate rows in one or both tables, provided that you have more than one row for the same date in one of the two tables or both.

What I was saying, would be something like this:

SELECT  I.FECHA,
        I.INVERSION_VALOR,
        G.GASTO_VALOR
        FROM (SELECT   FECHA,
                       SUM(VALOR) AS INVERSION_VALOR
                       FROM INVERSION
                       GROUP BY FECHA
              ) I
        LEFT JOIN (SELECT  FECHA,
                           SUM(VALOR) AS GASTO_VALOR 
                           FROM GASTOS
                           GROUP BY(FECHA)
              ) G
              ON G.FECHA = I.FECHA

This has an eventual problem, just as the query will bring you all rows of inversion and only those that match gastos . This would be easily solved if mysql had a FULL OUTER JOIN , unfortunately it is not so you have to emulate it in this way:

SELECT  I.FECHA,
        I.INVERSION_VALOR,
        G.GASTO_VALOR
        FROM (SELECT   FECHA,
                       SUM(VALOR) AS INVERSION_VALOR
                       FROM INVERSION
                       GROUP BY FECHA
              ) I
        LEFT JOIN (SELECT  FECHA,
                           SUM(VALOR) AS GASTO_VALOR 
                           FROM GASTOS
                           GROUP BY(FECHA)
              ) G
              ON G.FECHA = I.FECHA

UNION

SELECT  I.FECHA,
        I.INVERSION_VALOR,
        G.GASTO_VALOR
        FROM (SELECT   FECHA,
                       SUM(VALOR) AS INVERSION_VALOR
                       FROM INVERSION
                       GROUP BY FECHA
              ) I
        RIGHT JOIN (SELECT  FECHA,
                           SUM(VALOR) AS GASTO_VALOR 
                           FROM GASTOS
                           GROUP BY(FECHA)
              ) G
              ON G.FECHA = I.FECHA
        WHERE I.FECHA IS NULL

If in any of the two tables you do not have rows, keep in mind that obviously the columns of amounts will return null , if you prefer to show the value 0, you should use a IFNULL(G.GASTO_VALOR, 0) or IFNULL(G.INVERSION_VALOR, 0)

    
answered by 06.11.2018 / 00:00
source
0

Your problem may be that you handle Masters and Details, by not using the join in the master queries in a good way, the value is usually doubled, it does not mean that it is bad, rather that the value is doubled by some variable that it is different in detail that it does not carry in the teacher.

SELECT I.FECHA      AS INV_FECHA,
       SUM(I.VALOR) AS INVERSION_VALOR, 
       SUM(G.VALOR) AS GASTO_VALOR 
       FROM INVERSION AS I 
       INNER JOIN GASTOS AS G ON I.FECHA = G.FECHA 
       GROUP BY(I.FECHA)'

Verify the data with a Select * from ***** of your main table and your Join to find the problem.

    
answered by 05.11.2018 в 23:34