DELETE
	C
FROM
	EST_META_FILIAL_CPL C
	INNER JOIN (
		SELECT
			CD_EMP, 
			CD_FILIAL, 
			MES_META, 
			ANO_META,
			MAX(CD_META_FILIAL) AS CD_META_FILIAL,
			COUNT(*) AS CONTADOR
		FROM
			EST_META_FILIAL
		GROUP BY
			CD_EMP, 
			CD_FILIAL, 
			MES_META, 
			ANO_META
		HAVING
			COUNT(*) > 1) M ON
		C.CD_EMP = M.CD_EMP
		AND C.CD_FILIAL = M.CD_FILIAL
		AND C.CD_META_FILIAL = M.CD_META_FILIAL

WHILE @@ROWCOUNT > 0
BEGIN
	DELETE
		C
	FROM
		EST_META_FILIAL_CPL C
		INNER JOIN (
			SELECT
				CD_EMP, 
				CD_FILIAL, 
				MES_META, 
				ANO_META,
				MAX(CD_META_FILIAL) AS CD_META_FILIAL,
				COUNT(*) AS CONTADOR
			FROM
				EST_META_FILIAL
			GROUP BY
				CD_EMP, 
				CD_FILIAL, 
				MES_META, 
				ANO_META
			HAVING
				COUNT(*) > 1) M ON
			C.CD_EMP = M.CD_EMP
			AND C.CD_FILIAL = M.CD_FILIAL
			AND C.CD_META_FILIAL = M.CD_META_FILIAL
END


DELETE
	C
FROM
	EST_META_FILIAL C
	INNER JOIN (
		SELECT
			CD_EMP, 
			CD_FILIAL, 
			MES_META, 
			ANO_META,
			MAX(CD_META_FILIAL) AS CD_META_FILIAL,
			COUNT(*) AS CONTADOR
		FROM
			EST_META_FILIAL
		GROUP BY
			CD_EMP, 
			CD_FILIAL, 
			MES_META, 
			ANO_META
		HAVING
			COUNT(*) > 1) M ON
		C.CD_EMP = M.CD_EMP
		AND C.CD_FILIAL = M.CD_FILIAL
		AND C.CD_META_FILIAL = M.CD_META_FILIAL

WHILE @@ROWCOUNT > 0
BEGIN
	DELETE
		C
	FROM
		EST_META_FILIAL C
		INNER JOIN (
			SELECT
				CD_EMP, 
				CD_FILIAL, 
				MES_META, 
				ANO_META,
				MAX(CD_META_FILIAL) AS CD_META_FILIAL,
				COUNT(*) AS CONTADOR
			FROM
				EST_META_FILIAL
			GROUP BY
				CD_EMP, 
				CD_FILIAL, 
				MES_META, 
				ANO_META
			HAVING
				COUNT(*) > 1) M ON
			C.CD_EMP = M.CD_EMP
			AND C.CD_FILIAL = M.CD_FILIAL
			AND C.CD_META_FILIAL = M.CD_META_FILIAL
END