WITH CTE AS (
    SELECT *,
           ROW_NUMBER() OVER (PARTITION BY [Entities], [AttributeName]
                              ORDER BY (SELECT NULL)) AS rn
    FROM EHR.T_EHR_REFERENCE
)
DELETE FROM CTE WHERE rn > 1;

if exists
(
	select * from sys.indexes i where i.object_id = object_id('EHR.T_EHR_REFERENCE', 'U') and i.name = 'UI_T_EHR_REFERENCE'
)
	DROP INDEX UI_T_EHR_REFERENCE ON EHR.T_EHR_REFERENCE;
go
CREATE UNIQUE INDEX UI_T_EHR_REFERENCE ON EHR.T_EHR_REFERENCE([Entities], [AttributeName]);
go