I made a LEFT JOIN
considering as attributes ( CPF
, TEL1
, TEL2
and TEL3
). Why all these? Because the CPF
field often returns zeroed on one of the bases, so I'm considering the phone fields as well.
As the code below, I need to know if there is a simpler way to do this crossing.
SELECT
convert(date,A.DT_EMISSAO) as DT_EMISSAO
,A.EPS
,A.MAILING
,A.CPF_TITULAR
,A.DDD_TERMINAL
,A.TELEFONE_TERMINAL
,A.PRODUTO
,A.VERSAO
,A.CANAL_VENDAS
,COUNT(*) AS QTDE_REG
,LEFT(C.TELEFONE,2) + REPLICATE('0',9 - LEN(RIGHT(C.TELEFONE,8))) + RTRIM (RIGHT(C.TELEFONE,8)) AS TELEFONE
,C.NOME_ARQUIVO
,C.DATA_BASE
,L.STATUS_FV
FROM [10.125.172.152].[televendas].[txt].[FichaDeVendasConsolidada] A with (nolock)
LEFT JOIN Carga_Outbound C ON (CONVERT(VARCHAR,A.DDD_TERMINAL) + CONVERT (VARCHAR, A.TELEFONE_TERMINAL) = LEFT(C.TELEFONE,2) + REPLICATE('0',9 - LEN(RIGHT(C.TELEFONE,8))) + RTRIM (RIGHT(C.TELEFONE,8)))
OR
CONVERT(VARCHAR,A.DDD_TERMINAL) + CONVERT (VARCHAR, A.TELEFONE_TERMINAL) = C.TELEFONE
LEFT JOIN De_ParaVersao L ON A.VERSAO = L.VERSAO_FV
WHERE
(A.VERSAO not like '%MAPFRE%'
AND A.VERSAO not like '%MAFRE%'
AND A.VERSAO not like '%SVA%'
AND A.VERSAO not like '%PRODUTO%'
AND A.VERSAO NOT LIKE '%ACEITOU%'
AND A.VERSAO NOT LIKE '%NAO ACEITOU%'
AND A.VERSAO NOT LIKE '%Ponto Adicional%'
AND A.VERSAO NOT LIKE '%SEGURO%')
AND A.CANAL_VENDAS = 'OUTBOUND'
AND A.MAILING = '3420'
AND C.DATA_BASE IS NOT NULL
AND CONVERT(DATE,A.DT_EMISSAO) between
(select CONVERT(DATE,dateadd(dd,-(day(DATEADD(MONTH,-2, getdate()))-1),DATEADD(MONTH,-2, getdate()))))
AND
(select CONVERT(DATE,dateadd(s,-1,dateadd(mm,datediff(m,0,DATEADD(MONTH,0, getdate()))+1,0))))
GROUP BY
A.DT_EMISSAO,A.EPS,A.MAILING,A.CPF_TITULAR,A.DDD_TERMINAL,A.TELEFONE_TERMINAL,A.PRODUTO,A.VERSAO,A.CANAL_VENDAS,C.TELEFONE,C.NOME_ARQUIVO,C.DATA_BASE,L.STATUS_FV
ORDER BY
A.DT_EMISSAO,COUNT(*) DESC, C.DATA_BASE
---------------------------------------------------------
----:UNION CPF:-----
---------------------------------------------------------
UNION ALL
SELECT
convert(date,B.DT_EMISSAO) as DT_EMISSAO
,B.EPS
,B.MAILING
,B.CPF_TITULAR
,B.DDD_TERMINAL
,B.TELEFONE_TERMINAL
,B.PRODUTO
,B.VERSAO
,B.CANAL_VENDAS
,COUNT(*) AS QTDE_REG
,LEFT(D.TELEFONE,2) + REPLICATE('0',9 - LEN(RIGHT(D.TELEFONE,8))) + RTRIM (RIGHT(D.TELEFONE,8)) AS TELEFONE
,D.NOME_ARQUIVO
,D.DATA_BASE
,M.STATUS_FV
FROM (
SELECT
convert(date,A.DT_EMISSAO) as DT_EMISSAO
,A.EPS
,A.MAILING
,A.CPF_TITULAR
,A.DDD_TERMINAL
,A.TELEFONE_TERMINAL
,A.PRODUTO
,A.VERSAO
,A.CANAL_VENDAS
,COUNT(*) AS QTDE_REG
,LEFT(C.TELEFONE,2) + REPLICATE('0',9 - LEN(RIGHT(C.TELEFONE,8))) + RTRIM (RIGHT(C.TELEFONE,8)) AS TELEFONE
,C.NOME_ARQUIVO
,C.DATA_BASE
,L.STATUS_FV
FROM [10.125.172.152].[televendas].[txt].[FichaDeVendasConsolidada] A with (nolock)
LEFT JOIN Carga_Outbound C ON (CONVERT(VARCHAR,A.DDD_TERMINAL) + CONVERT (VARCHAR, A.TELEFONE_TERMINAL) = LEFT(C.TELEFONE,2) + REPLICATE('0',9 - LEN(RIGHT(C.TELEFONE,8))) + RTRIM (RIGHT(C.TELEFONE,8)))
OR
CONVERT(VARCHAR,A.DDD_TERMINAL) + CONVERT (VARCHAR, A.TELEFONE_TERMINAL) = C.TELEFONE
LEFT JOIN De_ParaVersao L ON A.VERSAO = L.VERSAO_FV
WHERE
(A.VERSAO not like '%MAPFRE%'
AND A.VERSAO not like '%MAFRE%'
AND A.VERSAO not like '%SVA%'
AND A.VERSAO not like '%PRODUTO%'
AND A.VERSAO NOT LIKE '%ACEITOU%'
AND A.VERSAO NOT LIKE '%NAO ACEITOU%'
AND A.VERSAO NOT LIKE '%Ponto Adicional%'
AND A.VERSAO NOT LIKE '%SEGURO%')
AND A.CANAL_VENDAS = 'OUTBOUND'
AND A.MAILING = '3420'
AND C.DATA_BASE IS NULL
AND CONVERT(DATE,A.DT_EMISSAO) between
(select CONVERT(DATE,dateadd(dd,-(day(DATEADD(MONTH,-2, getdate()))-1),DATEADD(MONTH,-2, getdate()))))
AND
(select CONVERT(DATE,dateadd(s,-1,dateadd(mm,datediff(m,0,DATEADD(MONTH,0, getdate()))+1,0))))
GROUP BY
A.DT_EMISSAO,A.EPS,A.MAILING,A.CPF_TITULAR,A.DDD_TERMINAL,A.TELEFONE_TERMINAL,A.PRODUTO,A.VERSAO,A.CANAL_VENDAS,C.TELEFONE,C.NOME_ARQUIVO,C.DATA_BASE,L.STATUS_FV
) B
LEFT JOIN Carga_Outbound D ON B.CPF_TITULAR = D.NR_CPF
LEFT JOIN De_ParaVersao M ON B.VERSAO = M.VERSAO_FV
WHERE
(B.VERSAO not like '%MAPFRE%'
AND B.VERSAO not like '%MAFRE%'
AND B.VERSAO not like '%SVA%'
AND B.VERSAO not like '%PRODUTO%'
AND B.VERSAO NOT LIKE '%ACEITOU%'
AND B.VERSAO NOT LIKE '%NAO ACEITOU%'
AND B.VERSAO NOT LIKE '%Ponto Adicional%'
AND B.VERSAO NOT LIKE '%SEGURO%')
AND B.CANAL_VENDAS = 'OUTBOUND'
AND B.MAILING = '3420'
AND D.DATA_BASE IS NOT NULL
AND CONVERT(DATE,B.DT_EMISSAO) between
(select CONVERT(DATE,dateadd(dd,-(day(DATEADD(MONTH,-2, getdate()))-1),DATEADD(MONTH,-2, getdate()))))
AND
(select CONVERT(DATE,dateadd(s,-1,dateadd(mm,datediff(m,0,DATEADD(MONTH,0, getdate()))+1,0))))
GROUP BY
B.DT_EMISSAO,B.EPS,B.MAILING,B.CPF_TITULAR,B.DDD_TERMINAL,B.TELEFONE_TERMINAL,B.PRODUTO,B.VERSAO,B.CANAL_VENDAS,D.TELEFONE,D.NOME_ARQUIVO,D.DATA_BASE,M.STATUS_FV
ORDER BY
B.DT_EMISSAO,COUNT(*) DESC, D.DATA_BASE
---------------------------------------------------------
----:UNION TELEFONE 1:-----
---------------------------------------------------------
UNION ALL
SELECT
convert(date,E.DT_EMISSAO) as DT_EMISSAO
,E.EPS
,E.MAILING
,E.CPF_TITULAR
,E.DDD_1
,E.TELEFONE_CONTATO_1
,E.PRODUTO
,E.VERSAO
,E.CANAL_VENDAS
,COUNT(*) AS QTDE_REG
,LEFT(F.TELEFONE,2) + REPLICATE('0',9 - LEN(RIGHT(F.TELEFONE,8))) + RTRIM (RIGHT(F.TELEFONE,8)) AS TELEFONE
,F.NOME_ARQUIVO
,F.DATA_BASE
,N.STATUS_FV
FROM (
SELECT
convert(date,B.DT_EMISSAO) as DT_EMISSAO
,B.EPS
,B.MAILING
,B.CPF_TITULAR
,B.DDD_TERMINAL
,B.TELEFONE_TERMINAL
,B.PRODUTO
,B.VERSAO
,B.CANAL_VENDAS
,COUNT(*) AS QTDE_REG
,LEFT(D.TELEFONE,2) + REPLICATE('0',9 - LEN(RIGHT(D.TELEFONE,8))) + RTRIM (RIGHT(D.TELEFONE,8)) AS TELEFONE
,D.NOME_ARQUIVO
,D.DATA_BASE
,M.STATUS_FV
FROM (
SELECT
convert(date,A.DT_EMISSAO) as DT_EMISSAO
,A.EPS
,A.MAILING
,A.CPF_TITULAR
,A.DDD_TERMINAL
,A.TELEFONE_TERMINAL
,A.PRODUTO
,A.VERSAO
,A.CANAL_VENDAS
,COUNT(*) AS QTDE_REG
,LEFT(C.TELEFONE,2) + REPLICATE('0',9 - LEN(RIGHT(C.TELEFONE,8))) + RTRIM (RIGHT(C.TELEFONE,8)) AS TELEFONE
,C.NOME_ARQUIVO
,C.DATA_BASE
,L.STATUS_FV
FROM [10.125.172.152].[televendas].[txt].[FichaDeVendasConsolidada] A with (nolock)
LEFT JOIN Carga_Outbound C ON (CONVERT(VARCHAR,A.DDD_TERMINAL) + CONVERT (VARCHAR, A.TELEFONE_TERMINAL) = LEFT(C.TELEFONE,2) + REPLICATE('0',9 - LEN(RIGHT(C.TELEFONE,8))) + RTRIM (RIGHT(C.TELEFONE,8)))
OR
CONVERT(VARCHAR,A.DDD_TERMINAL) + CONVERT (VARCHAR, A.TELEFONE_TERMINAL) = C.TELEFONE
LEFT JOIN De_ParaVersao L ON A.VERSAO = L.VERSAO_FV
WHERE
(A.VERSAO not like '%MAPFRE%'
AND A.VERSAO not like '%MAFRE%'
AND A.VERSAO not like '%SVA%'
AND A.VERSAO not like '%PRODUTO%'
AND A.VERSAO NOT LIKE '%ACEITOU%'
AND A.VERSAO NOT LIKE '%NAO ACEITOU%'
AND A.VERSAO NOT LIKE '%Ponto Adicional%'
AND A.VERSAO NOT LIKE '%SEGURO%')
AND A.CANAL_VENDAS = 'OUTBOUND'
AND A.MAILING = '3420'
AND C.DATA_BASE IS NULL
AND CONVERT(DATE,A.DT_EMISSAO) between
(select CONVERT(DATE,dateadd(dd,-(day(DATEADD(MONTH,-2, getdate()))-1),DATEADD(MONTH,-2, getdate()))))
AND
(select CONVERT(DATE,dateadd(s,-1,dateadd(mm,datediff(m,0,DATEADD(MONTH,0, getdate()))+1,0))))
GROUP BY
A.DT_EMISSAO,A.EPS,A.MAILING,A.CPF_TITULAR,A.DDD_TERMINAL,A.TELEFONE_TERMINAL,A.PRODUTO,A.VERSAO,A.CANAL_VENDAS,C.TELEFONE,C.NOME_ARQUIVO,C.DATA_BASE,L.STATUS_FV
) B
LEFT JOIN Carga_Outbound D ON B.CPF_TITULAR = D.NR_CPF
LEFT JOIN De_ParaVersao M ON B.VERSAO = M.VERSAO_FV
WHERE
(B.VERSAO not like '%MAPFRE%'
AND B.VERSAO not like '%MAFRE%'
AND B.VERSAO not like '%SVA%'
AND B.VERSAO not like '%PRODUTO%'
AND B.VERSAO NOT LIKE '%ACEITOU%'
AND B.VERSAO NOT LIKE '%NAO ACEITOU%'
AND B.VERSAO NOT LIKE '%Ponto Adicional%'
AND B.VERSAO NOT LIKE '%SEGURO%')
AND B.CANAL_VENDAS = 'OUTBOUND'
AND B.MAILING = '3420'
AND D.DATA_BASE IS NULL
AND CONVERT(DATE,B.DT_EMISSAO) between
(select CONVERT(DATE,dateadd(dd,-(day(DATEADD(MONTH,-2, getdate()))-1),DATEADD(MONTH,-2, getdate()))))
AND
(select CONVERT(DATE,dateadd(s,-1,dateadd(mm,datediff(m,0,DATEADD(MONTH,0, getdate()))+1,0))))
GROUP BY
B.DT_EMISSAO,B.EPS,B.MAILING,B.CPF_TITULAR,B.DDD_TERMINAL,B.TELEFONE_TERMINAL,B.PRODUTO,B.VERSAO,B.CANAL_VENDAS,D.TELEFONE,D.NOME_ARQUIVO,D.DATA_BASE,M.STATUS_FV
) E
LEFT JOIN Carga_Outbound F ON (CONVERT(VARCHAR,E.DDD_1) + CONVERT (VARCHAR,E.TELEFONE_CONTATO_1) = LEFT(F.TELEFONE,2) + REPLICATE('0',9 - LEN(RIGHT(F.TELEFONE,8))) + RTRIM (RIGHT(F.TELEFONE,8)))
OR
CONVERT(VARCHAR,E.DDD_1) + CONVERT (VARCHAR,E.TELEFONE_CONTATO_1) = F.TELEFONE
LEFT JOIN De_ParaVersao N ON E.VERSAO = N.VERSAO_FV
WHERE
(E.VERSAO not like '%MAPFRE%'
AND E.VERSAO not like '%MAFRE%'
AND E.VERSAO not like '%SVA%'
AND E.VERSAO not like '%PRODUTO%'
AND E.VERSAO NOT LIKE '%ACEITOU%'
AND E.VERSAO NOT LIKE '%NAO ACEITOU%'
AND E.VERSAO NOT LIKE '%Ponto Adicional%'
AND E.VERSAO NOT LIKE '%SEGURO%')
AND E.CANAL_VENDAS = 'OUTBOUND'
AND E.MAILING = '3420'
AND F.DATA_BASE IS NOT NULL
AND CONVERT(DATE,E.DT_EMISSAO) between
(select CONVERT(DATE,dateadd(dd,-(day(DATEADD(MONTH,-2, getdate()))-1),DATEADD(MONTH,-2, getdate()))))
AND
(select CONVERT(DATE,dateadd(s,-1,dateadd(mm,datediff(m,0,DATEADD(MONTH,0, getdate()))+1,0))))
GROUP BY
E.DT_EMISSAO,E.EPS,E.MAILING,E.CPF_TITULAR,E.DDD_1,E.TELEFONE_CONTATO_1,E.PRODUTO,E.VERSAO,E.CANAL_VENDAS,F.TELEFONE,F.NOME_ARQUIVO,F.DATA_BASE,N.STATUS_FV
ORDER BY
E.DT_EMISSAO,COUNT(*) DESC, F.DATA_BASE