1

I need to query a table for duplicate deposit records, where two deposits made at one cash terminal, for the same amount, within a certain time window, are considered duplicate records. I've started working on a query now, but I would appreciate any advice or suggestions on doing this 'properly'.

2 Answers 2

3

Simple aggregate

SELECT
   col1, col2, col3, ...
FROM
   MyTable
GROUP BY
   col1, col2, col3, ...
HAVING
   COUNT(*) >= 2

Don't include your identity/key/PK column: this will be unique per row and mess up the aggregate.

To get a row to remove or keep, do a MAX or MIN on that

SELECT
   col1, col2, col3, ...,
   MAX(IDCol) AS RowToDelete,
   MIN(IDCol) AS RowToKeep
FROM
   MyTable
GROUP BY
   col1, col2, col3, ...
HAVING
   COUNT(*) >= 2

Of course, with 3 duplicates then do a "keep".

Edit:

For rows within a time window, use a self join or window/ranking function

2
  • Yours is better for finding exact duplicates. Dec 10, 2010 at 8:30
  • Thanks, but to group on the difference between deposit times of two records will prove difficult. @Damien_The_Unbeliever has a more flexible query.
    – ProfK
    Dec 10, 2010 at 8:41
2

Generally, you'd do a self join to the same table, and put your "duplicate" criteria in the join conditions.

E.g.

SELECT
    *
FROM
    Transactions t1
        inner join
    Transactions t2
        on
            t1.Terminal = t2.Terminal and
            t1.Amount = t2.Amount and
            DATEDIFF(minute,t2.TransactionDate,t1.TransactionDate) between 0 and 10 and
            t1.TransactionID > t2.TransactionID /* prevent matching the same row */

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.