I am trying to perform the grouping of records of two tables using the following query:
select distinct detrec.iddestino,
--sum(DetRec.NoBulto) as [Número de Bultos], sum(DetRec.Peso) as [Peso],
case when enrec.OrigenEnvios=detrec.iddestino then sum(DetRec.NoBulto)
end AS NoBultoRecibido,
case when enrec.OrigenEnvios=detrec.iddestino then sum(DetRec.Peso)
end as Peso,
case when enrec.OrigenEnvios<>detrec.iddestino then sum(DetRec.Nobulto)
end as NoBultoEnviado,
case when enrec.OrigenEnvios<>detrec.iddestino then sum(DetRec.Peso)
end as Peso
from Recibos EnRec
inner join RecibosDetalle DetRec on (DetRec.IdRecibo = EnRec.IdRecibo)
inner join Destinos Dest on (Dest.IdDestino = DetRec.IdDestino)
where convert(char(15),fecharecibo,111) between '2017/01/13' and '2017/01/13' and enrec.idservicio='1'
group by EnRec.IdServicio,DetRec.Iddestino,Enrec.OrigenEnvios
The result I get from the previous query is the following:
# Figure1
And the one I'm looking for is:
# Figure2
I would appreciate knowing if there is any other way to achieve the result of #Figure2 , or what I am doing wrong.