Group duplicate lines Sql Server

1

I created the query where I have a Products table and another inventory (1: N) I need to add the stock of each product has in the inventory table, I made the following query:

select DISTINCT p.ID,IsNull(p.CodigoBarra, '') As 
CodBarra,IsNull(p.CodigoAlternativo, '') As 
CodAlt,p.Descricao,p.DescricaoAlternativa,f.Descricao as Fabricante, 
sum(est.EstoqueAtual) as EstoqueAtual, COUNT(est.EstoqueAtual) as 
QtdEsnderecosEstoque from produto p
inner join Fabricante f on f.ID = p.FabricanteID
left join EstoqueLoteLocalizacao est on est.ProdutoId = p.ID
where p.DataExclusao is null and p.EmpresaID = 3
group by p.ID,p.CodigoBarra,p.CodigoAlternativo,p.Descricao,p.DescricaoAlternativa,f.Descricao, est.EstoqueAtual

This Query returns the following:

What I need is that instead of it giving me the 2 records , it group and the current stock there is summed 4000 - -40 and me > display in 1 record only the value of 3960

    
asked by anonymous 23.06.2017 / 02:07

1 answer

1

When you use grouping function (such as SUM ), it will take into account all rows that are differentiated by GROUP BY .

In this case, its GROUP BY included in its clause the est.EstoqueAtual column. If you remove this column from GROUP BY , it will group it and it will be added as desired.

    
23.06.2017 / 02:18