Interview - Detect/remove duplicate entries

后端 未结 3 580
感情败类
感情败类 2020-12-11 13:29

how to detect/remove duplicate entries from a database in a table where there is no primary key ?

[If we use \'DISTINCT\' how do we know which record is the correct

3条回答
  •  庸人自扰
    2020-12-11 14:26

    I created a view where DISTINCT actually was not a part of the query, but PARTITION. I needed the most recent entry to records with the same Ordernum and RecordType fields, discarding the others. The partitions are ordered by date, and then the top row is selected, like this:

    SELECT *, ROW_NUMBER() 
    OVER (PARTITION BY OrderNum, RecordType ORDER BY DateChanged DESC) rn
    FROM HistoryTable SELECT * FROM q WHERE rn = 1
    

提交回复
热议问题