Sql query return the same name with different date

Asked

Viewed 96 times

1

My table Acidente is more or less like this:

Vitima - DataRegistro
Pedro  - 10/02/2015 
Pedro  - 20/03/2015
Maria  - 01/05/2015
Maria  - 01/05/2015

I need to make a query (Sql) that returns the records that contain:

  • Victim’s name appears more than once
  • With different record dates (in this case the last record will not return in the query)

1 answer

3


This query will only return Victims that appear at least more than once and with different dates, remembering that the correct one would be you seek for the ID of the Victim and not name as it may repeat.

SELECT DISTINCT a1.* FROM Acidentes a1
INNER JOIN Acidentes a2 ON a1.Vitima = a2.Vitima AND a1.DataRegistro <> a2.DataRegistro

Sqlfiddle Demo

  • Maicon, good morning. Thank you very much for your help, I kept breaking my head late Friday with this report.. abrass

Browser other questions tagged

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