Take duplicity by ignoring a column with distinct information

Asked

Viewed 147 times

0

I searched here and found nothing like it.

When I make the following Select :

SELECT  
    a.idEmpregadoReserva as Controle
    ,(select EmpresaAplicativo from tbEmpresaAplicativo where tbEmpresaAplicativo.idEmpresaAplicativo = c.idEmpresaAplicativo) Colonia
    ,d.Nome
    ,d.Matricula


    ,a.NumeroDocumento as [Numero Voucher]
    ,e.ValorVoucher
    ,f.TipoVoucher 
    ,c.idReserva
    ,(select tipoformapagto from tbHPG_TipoFormaPagto where tbHPG_TipoFormaPagto.idTipoFormaPagto = a.idTipoFormaPagto) [Forma Pagamento]
  FROM [APCEF].[dbo].[tbHPG_EmpregadoReserva_Financeiro] a join tbHPG_EmpregadoReserva_Reserva b on a.idEmpregadoReserva = b.idEmpregadoReserva
   join tbHPG_Reserva c on b.idReserva = c.idReserva join tbEmpregado d on c.idEmpregado = d.idEmpregado join tbEmpregadoVoucher e
    on a.NumeroDocumento = e.idEmpregadoVoucher
    join tbTipoVoucher f on e.idTipoVoucher = f.idTipoVoucher
  where c.DataEntrada between '2019-01-23' and '2019-01-27' and (c.idEmpresaAplicativo = 4 or c.idEmpresaAplicativo = 5) 
  and a.idTipoFormaPagto = 1

And the next thing :

inserir a descrição da imagem aqui

However I would like to make a select as if it were a select DISTINCT but ignoring IDRESERVA, ie pulling only 1 voucher number ( it can not repeat )

  • puts the select code for us please, can give crtl+c

  • @Germanobussniehues I made the substitution

2 answers

0

    SELECT  
        a.idEmpregadoReserva as Controle
        ,(select EmpresaAplicativo from tbEmpresaAplicativo where tbEmpresaAplicativo.idEmpresaAplicativo = c.idEmpresaAplicativo) Colonia
        ,d.Nome
        ,d.Matricula


        ,a.NumeroDocumento as [Numero Voucher]
        ,e.ValorVoucher
        ,f.TipoVoucher 
        ,wm_concat(c.idReserva) as ids_de_reservas
        ,(select tipoformapagto from tbHPG_TipoFormaPagto where tbHPG_TipoFormaPagto.idTipoFormaPagto = a.idTipoFormaPagto) [Forma Pagamento]
      FROM [APCEF].[dbo].[tbHPG_EmpregadoReserva_Financeiro] a join tbHPG_EmpregadoReserva_Reserva b on a.idEmpregadoReserva = b.idEmpregadoReserva
       join tbHPG_Reserva c on b.idReserva = c.idReserva join tbEmpregado d on c.idEmpregado = d.idEmpregado join tbEmpregadoVoucher e
        on a.NumeroDocumento = e.idEmpregadoVoucher
        join tbTipoVoucher f on e.idTipoVoucher = f.idTipoVoucher
      where c.DataEntrada between '2019-01-23' and '2019-01-27' and (c.idEmpresaAplicativo = 4 or c.idEmpresaAplicativo = 5) 
      and a.idTipoFormaPagto = 1

group by
 a.idEmpregadoReserva
        ,(select EmpresaAplicativo from tbEmpresaAplicativo where tbEmpresaAplicativo.idEmpresaAplicativo = c.idEmpresaAplicativo)
        ,d.Nome
        ,d.Matricula
        ,a.NumeroDocumento
        ,e.ValorVoucher
        ,f.TipoVoucher ,
      (select tipoformapagto from tbHPG_TipoFormaPagto where tbHPG_TipoFormaPagto.idTipoFormaPagto = a.idTipoFormaPagto)

If I understood your question and the structure of your bank would be this, I hope it works =D.

Note: If it is Mysql and does not work wm_concat for concat_ws

  • It did not work even with wm_concat and concat_ws and did not change the error reserve select in group by

0


See if this helps you, this query is for SQL SERVER

SELECT * FROM (SELECT  
    a.idEmpregadoReserva as Controle
    ,(select EmpresaAplicativo from tbEmpresaAplicativo where tbEmpresaAplicativo.idEmpresaAplicativo = c.idEmpresaAplicativo) Colonia
    ,d.Nome
    ,d.Matricula


    ,a.NumeroDocumento as [Numero Voucher]
    ,e.ValorVoucher
    ,f.TipoVoucher 
    ,c.idReserva
    ,(select tipoformapagto from tbHPG_TipoFormaPagto where tbHPG_TipoFormaPagto.idTipoFormaPagto = a.idTipoFormaPagto) [Forma Pagamento]
    ,row_number() over(partition by a.NumeroDocumento order by (select null)) as rn
  FROM [APCEF].[dbo].[tbHPG_EmpregadoReserva_Financeiro] a join tbHPG_EmpregadoReserva_Reserva b on a.idEmpregadoReserva = b.idEmpregadoReserva
   join tbHPG_Reserva c on b.idReserva = c.idReserva join tbEmpregado d on c.idEmpregado = d.idEmpregado join tbEmpregadoVoucher e
    on a.NumeroDocumento = e.idEmpregadoVoucher
    join tbTipoVoucher f on e.idTipoVoucher = f.idTipoVoucher
  where c.DataEntrada between '2019-01-23' and '2019-01-27' and (c.idEmpresaAplicativo = 4 or c.idEmpresaAplicativo = 5) 
  and a.idTipoFormaPagto = 1) A
  WHERE RN = 1
  • Dude worked, could you explain to me what was done with this sub select within select ? You sort of assigned the document number as a 'counter' and asked to show only the first ?

Browser other questions tagged

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