SQL Retornando linhas duplicadas
Boa Noite Pessoal!
Montei o SQL abaixo, porém quando inclui a condição CASE, está retornando linhas duplicadas, alguém pode ajudar?
Segue abaixo consulta:
SELECT DISTINCT FF.CODCOLIGADA,
FF.CHAPA,
P.NOME,
P.CODSITUACAO,
P.CODFILIAL,
P.CODSECAO,
RAT.CODCCUSTO,
CUST.NOME AS ''NOME CENTRO DE CUSTO'',
FF.MESCOMP,
FF.ANOCOMP,
FF.NROPERIODO,
FF.LIQUIDO,
SEC.DESCRICAO,
(CASE
WHEN MOV.CODEVENTO IN ( ''0075'' ) THEN MOV.VALOR
ELSE 0
END) AS ''GRATIFICACAO E PREMIO'' /*@Weverton GRATIFICACAO E PREMIO*/
FROM PFPERFF AS FF WITH (NOLOCK)
INNER JOIN PFUNC AS P WITH (NOLOCK)
ON P.CODCOLIGADA = FF.CODCOLIGADA
AND P.CHAPA = FF.CHAPA
INNER JOIN PFRATEIOFIXO AS RAT WITH (NOLOCK)
ON RAT.CODCOLIGADA = P.CODCOLIGADA
AND RAT.CHAPA = P.CHAPA
INNER JOIN GCCUSTO AS CUST WITH (NOLOCK)
ON CUST.CODCOLIGADA = RAT.CODCOLIGADA
AND CUST.CODCCUSTO = RAT.CODCCUSTO
INNER JOIN PFMOVCC MOV
ON FF.CODCOLIGADA = MOV.CODCOLIGADA
AND FF.CHAPA = MOV.CHAPA
AND FF.ANOCOMP = MOV.ANOCOMP
AND FF.MESCOMP = MOV.MESCOMP
AND FF.NROPERIODO = MOV.NROPERIODO
INNER JOIN PSECAO AS SEC
ON P.CODCOLIGADA = SEC.CODCOLIGADA
AND P.CODSECAO = SEC.CODIGO
WHERE ( FF.CODCOLIGADA = 1 )
AND ( FF.MESCOMP = 11 )
AND ( FF.ANOCOMP = 2021 )
AND ( FF.NROPERIODO = 2 )
GROUP BY FF.CODCOLIGADA,
FF.CHAPA,
P.NOME,
P.CODSITUACAO,
P.CODFILIAL,
P.CODSECAO,
RAT.CODCCUSTO,
CUST.NOME,
FF.MESCOMP,
FF.ANOCOMP,
FF.NROPERIODO,
FF.LIQUIDO,
SEC.DESCRICAO,
MOV.VALOR,
MOV.CODEVENTO
Montei o SQL abaixo, porém quando inclui a condição CASE, está retornando linhas duplicadas, alguém pode ajudar?
Segue abaixo consulta:
SELECT DISTINCT FF.CODCOLIGADA,
FF.CHAPA,
P.NOME,
P.CODSITUACAO,
P.CODFILIAL,
P.CODSECAO,
RAT.CODCCUSTO,
CUST.NOME AS ''NOME CENTRO DE CUSTO'',
FF.MESCOMP,
FF.ANOCOMP,
FF.NROPERIODO,
FF.LIQUIDO,
SEC.DESCRICAO,
(CASE
WHEN MOV.CODEVENTO IN ( ''0075'' ) THEN MOV.VALOR
ELSE 0
END) AS ''GRATIFICACAO E PREMIO'' /*@Weverton GRATIFICACAO E PREMIO*/
FROM PFPERFF AS FF WITH (NOLOCK)
INNER JOIN PFUNC AS P WITH (NOLOCK)
ON P.CODCOLIGADA = FF.CODCOLIGADA
AND P.CHAPA = FF.CHAPA
INNER JOIN PFRATEIOFIXO AS RAT WITH (NOLOCK)
ON RAT.CODCOLIGADA = P.CODCOLIGADA
AND RAT.CHAPA = P.CHAPA
INNER JOIN GCCUSTO AS CUST WITH (NOLOCK)
ON CUST.CODCOLIGADA = RAT.CODCOLIGADA
AND CUST.CODCCUSTO = RAT.CODCCUSTO
INNER JOIN PFMOVCC MOV
ON FF.CODCOLIGADA = MOV.CODCOLIGADA
AND FF.CHAPA = MOV.CHAPA
AND FF.ANOCOMP = MOV.ANOCOMP
AND FF.MESCOMP = MOV.MESCOMP
AND FF.NROPERIODO = MOV.NROPERIODO
INNER JOIN PSECAO AS SEC
ON P.CODCOLIGADA = SEC.CODCOLIGADA
AND P.CODSECAO = SEC.CODIGO
WHERE ( FF.CODCOLIGADA = 1 )
AND ( FF.MESCOMP = 11 )
AND ( FF.ANOCOMP = 2021 )
AND ( FF.NROPERIODO = 2 )
GROUP BY FF.CODCOLIGADA,
FF.CHAPA,
P.NOME,
P.CODSITUACAO,
P.CODFILIAL,
P.CODSECAO,
RAT.CODCCUSTO,
CUST.NOME,
FF.MESCOMP,
FF.ANOCOMP,
FF.NROPERIODO,
FF.LIQUIDO,
SEC.DESCRICAO,
MOV.VALOR,
MOV.CODEVENTO
Weverton Lemos
Curtidas 0
Respostas
Emerson Nascimento
21/02/2022
talvez seja necessário repetir o case no group by
GOSTEI 0
Weverton Lemos
21/02/2022
talvez seja necessário repetir o case no group by
Boa Noite! Então já tentei mais não consigo.... tentei de tudo.....SELECT FF.CODCOLIGADA, FF.CHAPA, P.NOME, P.CODSITUACAO, P.CODFILIAL, P.CODSECAO, RAT.CODCCUSTO, CUST.NOME AS 'NOME CENTRO DE CUSTO', FF.MESCOMP, FF.ANOCOMP, FF.NROPERIODO, FF.LIQUIDO, SEC.DESCRICAO, ( CASE WHEN MOV.CODEVENTO IN ( '0075' ) THEN MOV.VALOR ELSE 0 END ) AS 'GRATIFICACAO E PREMIO' /*@Weverton GRATIFICACAO E PREMIO*/ FROM PFPERFF AS FF WITH (NOLOCK) INNER JOIN PFUNC AS P WITH (NOLOCK) ON P.CODCOLIGADA = FF.CODCOLIGADA AND P.CHAPA = FF.CHAPA INNER JOIN PFRATEIOFIXO AS RAT WITH (NOLOCK) ON RAT.CODCOLIGADA = P.CODCOLIGADA AND RAT.CHAPA = P.CHAPA INNER JOIN GCCUSTO AS CUST WITH (NOLOCK) ON CUST.CODCOLIGADA = RAT.CODCOLIGADA AND CUST.CODCCUSTO = RAT.CODCCUSTO INNER JOIN PFMOVCC MOV ON FF.CODCOLIGADA = MOV.CODCOLIGADA AND FF.CHAPA = MOV.CHAPA AND FF.ANOCOMP = MOV.ANOCOMP AND FF.MESCOMP = MOV.MESCOMP AND FF.NROPERIODO = MOV.NROPERIODO LEFT OUTER JOIN PSECAO AS SEC ON P.CODCOLIGADA = SEC.CODCOLIGADA AND P.CODSECAO = SEC.CODIGO WHERE ( FF.CODCOLIGADA = :COLIGADA ) AND ( FF.MESCOMP = :MES ) AND ( FF.ANOCOMP = :ANO ) AND ( FF.NROPERIODO = :PERIODO ) GROUP BY FF.CODCOLIGADA, FF.CHAPA, P.NOME, P.CODSITUACAO, P.CODFILIAL, P.CODSECAO, RAT.CODCCUSTO, CUST.NOME, FF.MESCOMP, FF.ANOCOMP, FF.NROPERIODO, FF.LIQUIDO, SEC.DESCRICAO, MOV.CODEVENTO, MOV.VALOR ORDER BY P.NOME Sempre me perco nessas consultas grandes....
GOSTEI 0
Weverton Lemos
21/02/2022
talvez seja necessário repetir o case no group by
Bom Dia Emerson!
Já removi até o Group By e mesmo assim continua duplicando creio que o problema esteja no CASE, poderia me ajudar? Preciso trazer a coluna de eventos....
SELECT FF.CODCOLIGADA, FF.CHAPA, P.NOME, P.CODSITUACAO, P.CODFILIAL, P.CODSECAO, RAT.CODCCUSTO, CUST.NOME AS 'NOME CENTRO DE CUSTO', FF.MESCOMP, FF.ANOCOMP, FF.NROPERIODO, SEC.DESCRICAO, FF.LIQUIDO, ( CASE WHEN MOV.CODEVENTO IN ( '0075' ) THEN MOV.VALOR ELSE 0 END ) AS 'GRATIFICACAO E PREMIO' /*@Weverton GRATIFICACAO E PREMIO*/ FROM PFPERFF AS FF WITH (NOLOCK) INNER JOIN PFUNC AS P WITH (NOLOCK) ON P.CODCOLIGADA = FF.CODCOLIGADA AND P.CHAPA = FF.CHAPA INNER JOIN PFRATEIOFIXO AS RAT WITH (NOLOCK) ON RAT.CODCOLIGADA = P.CODCOLIGADA AND RAT.CHAPA = P.CHAPA INNER JOIN GCCUSTO AS CUST WITH (NOLOCK) ON CUST.CODCOLIGADA = RAT.CODCOLIGADA AND CUST.CODCCUSTO = RAT.CODCCUSTO INNER JOIN PFMOVCC MOV ON FF.CODCOLIGADA = MOV.CODCOLIGADA AND FF.CHAPA = MOV.CHAPA AND FF.ANOCOMP = MOV.ANOCOMP AND FF.MESCOMP = MOV.MESCOMP AND FF.NROPERIODO = MOV.NROPERIODO LEFT OUTER JOIN PSECAO AS SEC ON P.CODCOLIGADA = SEC.CODCOLIGADA AND P.CODSECAO = SEC.CODIGO WHERE ( FF.CODCOLIGADA = :COLIGADA ) AND ( FF.MESCOMP = :MES ) AND ( FF.ANOCOMP = :ANO ) AND ( FF.NROPERIODO = :PERIODO ) ORDER BY P.NOME
GOSTEI 0