Filter query by removing duplicates

Asked

Viewed 51 times

1

Good afternoon, I’m with the following select:

select  codemp, codfunc, codevento
, vlrevento 
from sankhya.TFPFOL
where codemp = 1 
and codfunc = 26
and month(referencia) = 1
and year(referencia) = 2009
group by codemp, codfunc, codevento
, vlrevento
having count(1)=1

that shows me the result below

inserir a descrição da imagem aqui

See that there are 2 fields with the codevento 534. I want to remove both but keep the vlrevento for others.

If I remove the vlrevento of GROUP BY He won’t allow me to complete the SELECT:

select  codemp, codfunc, codevento, vlrevento
from sankhya.TFPFOL
where codemp = 1 
and codfunc = 26
and month(referencia) = 1
and year(referencia) = 2009
group by codemp, codfunc, codevento

having count(1)=1
Mensagem 8120, Nível 16, Estado 1, Linha 22
A coluna 'sankhya.TFPFOL.VLREVENTO' é inválida na lista de seleção porque não está contida em uma função de agregação nem na cláusula GROUP BY.

and if I remove the vlrevento select obviously it does not show me in the result

inserir a descrição da imagem aqui

How can I inform the field vlrevento in that select without the appearance of codeventos that have duplicated value?

Grateful from now on

  • You have to report what you’re going to do with vlrevento. Do you want to show the biggest? The smallest? A sum?

  • 1

    In this case it would only be to inform the vlrevento corresponding to the codevento. As I want to remove the codevento that have duplicate value I do not need to show a min or max. The ones that are repeated I will not use

1 answer

2


Group the records and if you have more than one record we will remove it from our return using the clause HAVING:

SELECT codemp, codfunc, codevento, SUM(vlrevento)
  FROM sankhya.TFPFOL
 WHERE codemp = 1 
   AND codfunc = 26
   AND month(referencia) = 1
   AND year(referencia) = 2009
 GROUP BY codemp, codfunc, codevento
HAVING count(codemp) = 1
  • is that I don’t need those double codewinds. So if I just add the vlrevento they’ll still show up in the query

  • If the codevento have more than 1 record you want to remove all of this codevento of the return?

  • That’s right. If there’s any duplicate codevento I won’t need any of them

  • Try again with this edition.

  • That’s exactly what I needed. Thank you very much!

  • Okay, hugs!!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.