CypherNet CypherNet - 7 months ago 25
SQL Question

SQL SUM giving 2 rows. I want 1 row

I am having difficulty in getting this query to return the sum in 1 row.

SELECT
st.[code], SUM(whst.WHQtyOnHand)
FROM
dbo.[StkItem] st, dbo.WhseStk whst
WHERE
whst.WHWhseID in (3, 4)
AND whst.WHStockLink = st.StockLink
AND st.Code = 'BNV203N'
GROUP BY
st.[Code], whst.WHWhseID, whst.WHStockLink


Here is the result I get:

Code WHQtyOnHand
---------------------------
BNV203N 694
BNV203N 3748


I need them as a total.

Answer

Change this line:

GROUP BY st.[Code], whst.WHWhseID, whst.WHStockLink

to

GROUP BY st.[Code]

When you group by WHWhseID, you break out those rows.