Sql query return same name with different date

1

My table Acidente looks something 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)
asked by anonymous 22.06.2015 / 03:41

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 to search for ID of Victim and not name, / p>

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

SqlFiddle Demo

    
22.06.2015 / 03:55