Select data in three Mysql tables and bring the latest result with the sum

Asked

Viewed 43 times

1

I am trying to join data from three tables using this sql:

select c.NOME,c.CPF,e.DATA_CAD ,e.ENFERECO as ENDEREÇO_Atual , sum(f.VALOR)
from cli1 as c
inner join end1 as e 
on c.IDCLI1 = e.ID_END
inner join fat1 as f
on f.ID_FATEND1 = e.IDEND1
where CPF = '12345678912'
order by e.ENFERECO desc;

The problem is that it returns the data with the first registered address, and I wanted to return with the last registered address and the date of change related to it and the total amount invoiced.

Example of the result: inserir a descrição da imagem aqui

How can I proceed in this case?

  • use order by e.DATA_CAD desc, e.ENFERECO desc?

  • This form also does not return with the date and the most recent address, returns the first date and address registered with the sum of the invoiced values.

  • mounts an example with data to make it easier to help. It can be in sqlfiddle

  • Follow the Link sqlfiddle code

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.