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.
- --Deduplicate Matter Participating Entites
 
- ;WITH P AS 
 - (
 - 	SELECT
 - 		Id,
 - 		ROW_NUMBER() OVER
 - 			(PARTITION BY Final_Matter_Id, Final_Participant_Type, Final_Participant_Kind, Final_Participant_Role, Final_Participant_Id ORDER BY [Row] DESC) DupNumber
 - 	FROM
 - 		__M_Matters_Participating_Entities
 - )
 - UPDATE
 - 	mpe
 - SET
 - 	Final_Status = 'Deleted',
 - 	Comment = CONCAT('Duplicate: ', P.DupNumber)
 - FROM
 - 	__M_Matters_Participating_Entities mpe
 - 		INNER JOIN
 - 	P
 - 		ON mpe.Id = P.Id
 - WHERE 1=1
 - 	AND DupNumber>1