Delete from sSetup.tOrgGroupEIS Where OrgGroupId <> (Select id from sSetup.tOrgGroupMaster Where id > 1)

DECLARE @lOrgGroupMaster TABLE (
	Id INT identity(1, 1)
	,OrgGroupId INT
	);

INSERT INTO @lOrgGroupMaster
Select MapOrgGroupId from  sSetup.tOrgGroupEIS;

DECLARE @lCount INT = 0;

SELECT @lCount = COUNT(OrgGroupId)
FROM sSetup.tOrgGroupEIS;

WHILE (@lCount > 0)
BEGIN
	DECLARE @lOrgGroupId INT = 0;

	SELECT @lOrgGroupId = OrgGroupId
	FROM @lOrgGroupMaster
	WHERE Id = @lCount

	--SELECT @lOrgGroupId
	UPDATE [sSetup].[tOrgGroupEIS] SET Title = (Select Title from  nverp.serp.tcustomermaster Where OrgGroupId = @lOrgGroupId) Where MapOrgGroupId = @lOrgGroupId
	--Select Title from  nverp.serp.tcustomermaster Where OrgGroupId = @lOrgGroupId

	SET @lCount = @lCount - 1
END;
--
UPDATE [sSetup].[tOrgGroupEIS] SET Title = MapApiUrl Where Title is null;
--
WITH CTE AS (
    SELECT MapOrgGroupId,
           RN = ROW_NUMBER() OVER (PARTITION BY MapOrgGroupId ORDER BY MapOrgGroupId)
    FROM sSetup.tOrgGroupEIS 
)
DELETE FROM CTE WHERE RN > 1;
--
Select * from [sSetup].[tOrgGroupEIS]
GO


