Detecting Duplicates by Reference Code

Detecting Duplicates by Reference Code

In some scenarios, you need to ensure that a reference code is completely unique among a record type.

 
To do this, use the following SQL script:

  1. --Look for duplicate records in the __M_Matters table
  2. SELECT
  3. *
  4. FROM
  5. --Change this to the table we want to identify duplicates in
  6. __M_Matters V1,
  7. (
  8. SELECT
  9. Final_ReferenceCode,
  10. COUNT(*) as Duplicates
  11. FROM
  12. __M_Matters --This needs to match the same table as above
  13. GROUP BY
  14. Final_ReferenceCode
  15. HAVING
  16. COUNT(*) > 1
  17. ) V2
  18. WHERE 1=1
  19. AND V1.Final_ReferenceCode = V2.Final_ReferenceCode
  20. ORDER BY
  21. V1.Final_ReferenceCode