Deduplicate Matter Participating Entities

Deduplicate Matter Participating Entities

Some systems will have the same entity (contact, user, etc...) associated with a matter multiple times with exactly the same role.  If the signature of the relationship is exactly the same, the following script will eliminate the duplicates.

  1. --Deduplicate Matter Participating Entites

  2. ;WITH P AS 
  3. (
  4. SELECT
  5. Id,
  6. ROW_NUMBER() OVER
  7. (PARTITION BY Final_Matter_Id, Final_Participant_Type, Final_Participant_Kind, Final_Participant_Role, Final_Participant_Id ORDER BY [Row] DESC) DupNumber
  8. FROM
  9. __M_Matters_Participating_Entities
  10. )
  11. UPDATE
  12. mpe
  13. SET
  14. Final_Status = 'Deleted',
  15. Comment = CONCAT('Duplicate: ', P.DupNumber)
  16. FROM
  17. __M_Matters_Participating_Entities mpe
  18. INNER JOIN
  19. P
  20. ON mpe.Id = P.Id
  21. WHERE 1=1
  22. AND DupNumber>1