0

I have this query

SELECT
A.cod_material,
SUM(B.cantidad_pedido) as cantidad_pedido
FROM materiales AS A LEFT JOIN ingreso_material AS B 
on A.cod_material = B.cod_material GROUP BY A.cod_material;  

My results are

cod_material    cantidad_pedido
--------------------------------
321010001             NULL
321010002             25125
321010004             1283
321010006             NULL

And I want it to show instead of NULL (0)

321010001               0

Ty for your help

4

2 回答 2

1

Use COALESCE() here. The function returns the first non-NULL value.

SELECT
  A.cod_material,
  COALESCE(SUM(B.cantidad_pedido), 0) as cantidad_pedido
FROM materiales AS A LEFT JOIN ingreso_material AS B 
  on A.cod_material = B.cod_material GROUP BY A.cod_material;  
于 2013-08-22T17:12:55.307 回答
1

用一个case

SUM(case when B.cantidad_pedido IS NULL 
         then 0 
         else B.cantidad_pedido 
    end) as cantidad_pedido

或者isnull

SUM(ISNULL(B.cantidad_pedido, 0)) as cantidad_pedido
于 2013-08-22T17:10:58.877 回答