0

I have this select query, I have a problem finding the right way to retrieve the values on the same row after a join, basically for every record of the main table (strumenti) there are multiples rapporti_di_verifica, I need to select for every row of strumenti the row of rapporti_di_verifica with the most recent date (which is called data_di_verifica).

Right now I retrieve this using MAX(data_verifica) and it works, but then I needed another field of the same table, id_esito (which becomes nome_esito after the last INNER JOIN). How can I select the id_esito that is on the same row of the most recent data_verifica?

I tried with an AND in the "spaced" INNER JOIN but I really couldn't figure out how to do it.

SELECT id_strumento, codice_interno, nome_tipologia, nome_locazione, nome_utilizzatore, MAX(data_verifica), nome_esito, nome_stato, serial_number,
DATE_ADD(MAX(data_verifica), INTERVAL rinnovo_ogni DAY) AS data_prossima_verifica
    FROM strumenti
    INNER JOIN tipologie ON strumenti.id_tipologia = tipologie.id_tipologia
    INNER JOIN stati ON strumenti.id_stato = stati.id_stato
    LEFT JOIN locazioni ON strumenti.id_locazione = locazioni.id_locazione
    LEFT JOIN utilizzatori ON strumenti.id_utilizzatore = utilizzatori.id_utilizzatore

    INNER JOIN rapporti_di_verifica ON strumenti.id_strumento = rapporti_di_verifica.id_strumento_verificato

    INNER JOIN esiti ON rapporti_di_verifica.id_esito = esiti.id_esito
    ORDER BY data_prossima_verifica, codice_interno
Favo02
  • 36
  • 5

0 Answers0