IF EXISTS(SELECT  * FROM  sys.views WHERE object_id = OBJECT_ID(N'[dbo].V_CONCILIACAO_CARTOES_LINX_CONCILIADOR_STATUS_CONCILIACAO'))
    BEGIN
        DROP VIEW  [dbo].[V_CONCILIACAO_CARTOES_LINX_CONCILIADOR_STATUS_CONCILIACAO];
    END;
GO

CREATE VIEW V_CONCILIACAO_CARTOES_LINX_CONCILIADOR_STATUS_CONCILIACAO 
AS
SELECT 
       A.IDT_LOTE_REDE
     , A.V1 AS QTDE_REG_LOTE
     , ISNULL(B.V2, 0) AS QTDE_REG_CONC
     , CASE WHEN A.V1 = ISNULL(B.V2, 0) THEN 1
           ELSE 0
       END AS STS_LOTE
FROM
	(SELECT C_A.IDT_LOTE_REDE, COUNT(C_A.CD_CTR) AS V1
		FROM CONCILIACAO_CARTOES_LINX_CONCILIADOR C_A
		GROUP BY C_A.IDT_LOTE_REDE) A
	LEFT JOIN(SELECT 
					   C_A.IDT_LOTE_REDE
					 , COUNT(C_B.CD_CTR) AS V2
				FROM 
					 CONCILIACAO_CARTOES_LINX_CONCILIADOR C_A
					 INNER JOIN CONCILIACAO_CARTOES_FINAL C_B ON C_A.CD_EMP = C_B.CD_EMP
																 AND C_A.CD_FILIAL = C_B.CD_FILIAL
																 AND C_A.DT_VENDA = C_B.DT_VD
																 AND CONVERT(VARCHAR,C_A.NSU_LOJA) = C_B.NSU_SITEF
				GROUP BY 
						 C_A.IDT_LOTE_REDE) B ON 
	A.IDT_LOTE_REDE = B.IDT_LOTE_REDE;