There are no rows for this product_id. Result returns no rows instead of SUM = 0.
SELECT COALESCE(SUM(amount), 0) FROM store2product WHERE product_id = 6706434 GROUP BY product_id;
Is there a way to get result = 0?
There is no record for product_id = 6706434 in table store2product. As you group by product_id, you get one result row per product_id found with this query. As the product_id is not found, no row is returned.
Simple solution: remove GROUP BY.
SELECT
COALESCE(SUM(amount), 0)
FROM store2product
WHERE product_id = 6706434;
Now you get one result row in any case.
Try this Trick..
Schema for your case
CREATE TABLE #TAB (ID INT, AMT DECIMAL(18,2))
INSERT INTO #TAB
SELECT 1,1200
UNION ALL
SELECT 1,120
UNION ALL
SELECT 3, 100
Now query the table like
SELECT SUM(ISNULL(AMT,0)) AS AMT FROM (
SELECT ID, SUM(AMT)AMT FROM #TAB WHERE ID =2 GROUP BY ID
UNION ALL
SELECT NULL AS ID, NULL AS AMT
)A
amountdefinitely a numeric field?COALESCEwill only return zero ifSUMreturns NULL, which presumably it is not.