Query Excluding duplicate records in SQL

0

I have a query in SQL server:

SELECT  [nIdLogTG]
      ,[nUsuario]
      ,[nConsDetalle]
      ,[nConsDoOrigen]
      ,(case when a.cCodigoComplementarioDo IS NULL THEN b.cCodigoComplementarioDo
      ELSE a.cCodigoComplementarioDo END) AS CodigoComplementarioDo,
      (case when c.CNombreCompleto IS NULL THEN d.CNombreCompleto
      ELSE c.CNombreCompleto END) AS NombreCompletoCliente
      ,[nConsDoDestino]
      ,(case when e.cCodigoComplementarioDo IS NULL THEN f.cCodigoComplementarioDo
      ELSE e.cCodigoComplementarioDo END) AS CodigoComplementarioDoDestino
      ,[dFechaLog]
      ,[NroCausacion]
      ,[NombreGasto]
      ,[Valor]
  FROM tbLog_TrasladodeGastos
   left join tbhencabezadodo a on tbLog_TrasladodeGastos.nConsDoOrigen = a.NConsDo
   left join tbEncabezadoDo b on tbLog_TrasladodeGastos.nConsDoOrigen = b.NConsDo
   left join tbCliente c on a.CCliente = c.CNit
   left join tbCliente d on b.CCliente = d.CNit
   left join tbhencabezadodo e on tbLog_TrasladodeGastos.nConsDodestino = e.NConsDo
   left join tbEncabezadoDo f on tbLog_TrasladodeGastos.nConsDodestino = f.NConsDo
   where dFechaLog > '2018-01-01'

Which brings me the following result:

and I need you not to show codes complementarios DO repeated, or NconsDOrigen with repeated values

I appreciate the help you can give me.

    
asked by Jhohan David 18.12.2018 в 22:13
source

1 answer

1

By what you see in the example table the records that have the complementary code DO repeated have all the other fields repeated, so that you could add at the end a:

GROUP BY nConsDoOrigen, CodigoComplementarioDo, NombreCompletoCliente, nConsDoDestino, CodigoComplementarioDoDestino

You could even add to the select one:

COUNT(CodigoComplementarioDo) AS numero_registros

With that you would see the number of repetitions associated with each ComplementaryCodeDo

I hope to contribute

    
answered by 19.12.2018 в 14:20