0
I performed a LEFT JOIN
considering as attributes (CPF
, TEL1
, TEL2
and TEL3
). Why all these? Why the field CPF
often returns zeroed in one of the bases, so I’m considering the phone fields as well.
As code below, I need to know if there is a simpler way to perform 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
You need to make your question more objective. Try to explain what this load table does? Which attributes between the two tables have a relationship ?
– Rafael Salomão
To consider a phone seems to be a gambit. Leaving that aside, you want the combination to work with the junction of Cpf or, null case on return, with any combination of the three return phones with any of the three charge phones?
– Jefferson Quesado
That’s right, Jefferson...really, in the ideal world, would be the CPF, but it’s not the reality, I don’t have the CPF on all return bases.
– Edilaine Martins
Rafael, the attributes of the table, is in the image above. Cargo = mailing (Customer base for a call center to perform sales). Return = Base of sales made in mailing. Attributes between tables = (CPF, TEL 1, TEL 2 and TEL 3). Why these 4? Inside the tbl_Return comes often with the CPF reset. So I decided to perform the crossing by phones.
– Edilaine Martins
I don’t understand :-(
– igventurelli
@Edilainemartins: In each of the tables there are individual columns for TEL1, TEL2 and TEL3? If so, what are the names of these columns? In the code only appears TELEFONE_TERMINAL (sales) and TELEPHONE (charge). // The crossing should be by comparing the set {TEL1, TEL2, TEL3} of the two tables OR should be individually, as in a Cartesian product (TEL1 sales with TEL1 load, then TEL2 load after TEL3 load; TEL2 sales with TEL1 load, then TEL2 load after TEL3 load etc) ?
– José Diz
@Edilainemartins: It would be useful if you added in the topic description, for each table, the name of the columns directly involved, how they are declared, what it contains etc. // Why is it necessary to add '0' in the phone number, when it has 8 digits? // Does each table have a column that acts as a primary key? // What is the approximate number of rows in each table? // The columns with phone, in the load table, are always correctly filled, without extra spaces?
– José Diz