Search code examples
oracle-databaseselectduplicatesdistinct

After running SELECT DISTINCT, duplicates still show


I am still seeing duplicates in my results after running the SELECT DISTINCT despite using all the appropriate techniques to insure duplicate rows are truly duplicate (applying trim to all fields, formatting the date fields as date only etc.). I even tried GROUP BY, yet after running it, duplicates still showed up. Does anyone have any clue what in the world is happening, and anything I can do?

SELECT DISTINCT 
    ID, Address_Line_1, Address_Line_2, City, State, Zip, 
    to_date(START_DATE, 'DD-MON-YYYY') as START_DATE, 
    to_date(END_DATE, 'DD-MON-YYYY') as END_DATE 
FROM 
    AddressHistory
ORDER BY 
    ID, START_DATE DESC;

enter image description here


Solution

  • Try

    SELECT DISTINCT 
         ID, Address_Line_1, Address_Line_2, City, State, Zip, 
         trunc(to_date(START_DATE, 'DD-MON-YYYY')) as START_DATE, 
         trunc(to_date(END_DATE, 'DD-MON-YYYY')) as END_DATE  FROM 
    AddressHistory 
    ORDER BY ID, START_DATE DESC;
    

    This truncates the times of the date and is faster than type cast conversion.

    What data type is START_DATE in your example? Perhaps you can get rid of the type cast completely?