How to avoid duplicate records in a mysql query

3

I have a table where I store companies. Then I have a table movements where I insert several effective amounts / bank and the id of a company from the company table.

Structure of the company table:

idCuCorriente |     nBanco      |  
---+--------------+-----------------
1             | Banco 1         |
2             | Banco 2         | 
3             | Banco 3         | 

Structure of the Movement table

idCaja |  idCuCorriente |  mov_efectivo |  saldo_efectivo   |  mov_banco   |  saldo_banco   |  
    ---+--------------+---------------------------------------------------------------------------
    1  | 1              | 1,0000        | 1,0000            | 0.00         | 0.00
    2  | 2              | 0.00          | 0.00              | 500.00       | 500.00
    3  | 2              | 0.00          | 0.00              | 400.00       | 400.00

What I want to list is the most recent record of each company (idCuCorriente) The result would have to leave me like this:

idCaja |  idCuCorriente |  mov_efectivo |  saldo_efectivo   |  mov_banco   |  saldo_banco   |  
    ---+--------------+---------------------------------------------------------------------------
    1  | 1              | 1,0000        | 1,0000            | 0.00         | 0.00
    3  | 2              | 0.00          | 0.00              | 400.00       | 400.00

How can I do the SELECT to get this result?

    
asked by Luis 06.12.2018 в 21:13
source

2 answers

7

The main thing in your query is to get the last movement of each idCuCorriente , for the data you show, I understand that the way is to look at the last idCaja , so this would be something like this:

SELECT  idCuCorriente,
        MAX(idCaja) idCaja
        FROM Movimiento
        GROUP BY idCuCorriente

Now, knowing what the last movement is, we can incorporate this as a subquery:

SELECT  M.idCaja,
        M.idCuCorriente,
        M.mov_efectivo,
        M.saldo_efectivo,
        M.mov_banco,
        M.saldo_banco
    FROM Empresa E
    INNER JOIN (SELECT  idCuCorriente,
                MAX(idCaja) idCaja
            FROM Movimiento
            GROUP BY idCuCorriente
        ) U
        ON U.idCuCorriente = E.idCuCorriente
    INNER JOIN Movimiento M
        ON M.idCuCorriente = U.idCuCorriente
        AND M.idCaja = U.idCaja

Some additional comments:

  • Strictly speaking, the rows you want to remove are not duplicated
  • Using a Id that is incremental in time either because it is an autonumeric or because it is maintained from the system is a quick way to get the last record, but if we want to be purists, a id should not indicate this temporality of the rows, should make a date, in which case the query is similar but a little more complex, since you would have to calculate the maximum date and then go there to find the id .
  • This solution is quite "ansi SQL", possibly there are better alternatives for MySql.
answered by 06.12.2018 / 21:26
source
1

I would not know how to bring only the last update because you are not keeping a date or anything where you can know which was the last insertion, I would not know if the IDCaja would be useful ...

But to begin with, you can try this:

SELECT DISCTINT * FROM Movement NATURAL JOIN Company;

Now we can start editing the answer until we get to the concrete one.

    
answered by 06.12.2018 в 21:29