Duplicate records in SQL - Oracle

Asked

Viewed 1,799 times

1

Good morning, I’m new here and I’ve got a boring problem. Distinct does not solve my problem because the duplication is due to the fact that I had to make the relationship with other tables to seek information. SQL returns me two values with different supervisors for the same IDCALLED, as I do to display only one IDCALLED so as not to duplicate the records?

SELECT ch.idchamado,su.nome AS nomesupervisor,
  e.nome,
  ch.dataabertura,
  ch.datafechamento,
  ta.desctipoassunto
FROM sase_chamado ch
LEFT JOIN vw_app_consulta_escolas e
ON ch.idpj = e.idpessoa_juridica
LEFT JOIN sase_tipoassunto ta
ON ta.idtipoassunto = ch.idtipoassunto
LEFT JOIN sase_escolasusuario eu
ON eu.idpj = ch.idpj
INNER JOIN sase_usuario su
ON eu.idusuario = su.idusuario
WHERE ch.dataabertura BETWEEN to_date('01/01/16', 'dd/mm/yy') AND to_date('30/07/16', 'dd/mm/yy')
ORDER BY su.nome,
  e.nome,
  ta.desctipoassunto

the darn duplicity is of that kind here:

inserir a descrição da imagem aqui

  • Do you just want an idchamado no matter how many su.nome you have? this goes for the rest of the fields?

  • Yes, in order not to bring 2 equal named. the only different field when duplicated is the supervisor’s own name.

  • Samuel, if you do not want to generate a separate line for each supervisor you cannot show the result of the su.nome field in your SELECT (you must also remove from the ORDER BY clause and apply DISTINCT or GROUP BY). If you want to show his name with this structure you will always show 1 line for each supervisor name. It is possible (I don’t know specifically if in Oracle it is) to also join the name of N supervisors in a single column and row, but then you should change your SELECT considerably.

  • Turns out I can’t take your name off SELECT because I need it to assign the call there’s a supervisor’s name. distinct only works when the record is fully equal, but in my case the name of the supervisor changes when it duplicates, so do not funf

  • In this case you can not group the lines really and should recover both results, if you have two lines (if I understood the logic of the query) is why there are two calls, each to a supervisor, and the other data are identical.

  • I just don’t know how to solve this kkk duplicity

Show 1 more comment

1 answer

0


When we have we want only one line without caring about the content we use MIN or MAX and group what is equal.

SELECT ch.idchamado AS idchamado, 
  MIN( su.nome ) AS nomesupervisor,
  e.nome,
  ch.dataabertura,
  ch.datafechamento,
  ta.desctipoassunto
FROM sase_chamado ch
LEFT JOIN vw_app_consulta_escolas e
ON ch.idpj = e.idpessoa_juridica
LEFT JOIN sase_tipoassunto ta
ON ta.idtipoassunto = ch.idtipoassunto
LEFT JOIN sase_escolasusuario eu
ON eu.idpj = ch.idpj
INNER JOIN sase_usuario su
ON eu.idusuario = su.idusuario
WHERE ch.dataabertura BETWEEN to_date('01/01/16', 'dd/mm/yy') AND to_date('30/07/16', 'dd/mm/yy')
GROUP BY 
  ch.idchamado 
  e.nome,
  ch.dataabertura,
  ch.datafechamento,
  ta.desctipoassunto
ORDER BY su.nome,
  e.nome,
  ta.desctipoassunto
  • Reginaldo, if the date I had as Datetime your group by will not work very well.

  • True. He could put it as out of GROUP BY and MIN too.

  • ORA-00933: SQL command not closed properly 00933. 00000 - "SQL command not properly ended" *Cause: *Action: Line error: 19 Column: 3

  • I managed to fix this error by adding your name in GROUP BY, but it keeps bringing duplicate.

  • Post the result for us to see.

  • posted the img of duplicity friend, Obrigad

  • Name is string min does not work

  • name is varchar2

  • It does work @Marconcíliosouza. I just tested it. It’s unbelievable that Samuel is running this select with MIN and GROUP BY.

  • What do you recommend Reginaldo to resolve this? Is there any way I can limit the query to not displaying the same IDCALLED twice?

  • Veja assim: 
SELECT MIN( ch.idchamado ) AS idchamado, 
 MIN( su.nome ) AS nomesupervisor,
 MIN( e.nome ) AS nome,
 MIn( ch.dataabertura ) AS dataabertura,
 MIN( ch.datafechamento ) AS datafechamento,
 MIN( ta.desctipoassunto ) AS desctipoassunto

  • Take out the entire GROUP BY clause

  • Notice that you are taking MIN from all fields. I don’t know the logic of your program, but there may be situations that will be coming more than one record and you will be merging between them and always getting the lowest value for all fields. This may be desirable in some conditions but can be dangerous and bring consequences.

  • But if I remove the group by clause will give error because min is group function then group by would be mandatory, or I’m wrong?

  • This is wrong. MIN, MAX, SUM are aggregate functions and do not need to be in GROUP BY, alias if MIN and/or MAX does not work. Alias, it makes me think that you put GROUP BY in all fields and MIN the answer didn’t work. I hope I didn’t confuse you. rsrs

  • Hello Reginaldo, this last way you spoke it only brings 1 record of the 254 existing, in case it brings only the IDCHAMADO 1 and nothing else.

  • I edited the answer. Run like this there.

  • With some adjustments in your last SQL I managed to get the expected result, thank you very much Reginaldo, have a great week, hugs!

Show 13 more comments

Browser other questions tagged

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