Monday, 9 September 2013

Getting correct result with Group By query in SQL Server

Getting correct result with Group By query in SQL Server

I have the following query
with CTE as
(
select Barkod, sum(kolicina) as Kolicina
from stocks
where Barkod = '555'
group by Barkod
)
select s.Barkod, s.Kategorija, s.Artikal, s.Opis, s.Kolicina, s.N_cena,
s.N_Iznos, s.P_cena, s.P_Iznos, s.datum, s.Golemina from Stocks as s
join CTE as b
on b.Barkod = s.Barkod
The results from this query is
555 ÊÎØÓËÀ QWRSF QWRSF 10 10.00 NULL 20.00 NULL
NULL NULL
555 ÊÎØÓËÀ QWRSF QWRSF 1 10.00 NULL 20.00 NULL
NULL NULL
I need to get the following result
555 ÊÎØÓËÀ QWRSF QWRSF 11 10.00 NULL 20.00 NULL
NULL NULL
So I need to sum up the Kolicina field and get all in one row.

No comments:

Post a Comment