184

Say I have a simple function in MySQL:

SELECT SUM(Column_1)
FROM Table
WHERE Column_2 = 'Test'

If no entries in Column_2 contain the text 'Test' then this function returns NULL, while I would like it to return 0.

I'm aware that a similar question has been asked a few times here, but I haven't been able to adapt the answers to my purposes, so I'd be grateful for some help to get this sorted.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Nick
  • 4,304
  • 15
  • 69
  • 108
  • possible duplicate of [Help with MySQL SUM()](http://stackoverflow.com/questions/1318640/help-with-mysql-sum) – JohnFx Sep 29 '11 at 19:18

4 Answers4

379

Use COALESCE to avoid that outcome.

SELECT COALESCE(SUM(column),0)
FROM   table
WHERE  ...

To see it in action, please see this sql fiddle: http://www.sqlfiddle.com/#!2/d1542/3/0


More Information:

Given three tables (one with all numbers, one with all nulls, and one with a mixture):

SQL Fiddle

MySQL 5.5.32 Schema Setup:

CREATE TABLE foo
(
  id    INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  val   INT
);

INSERT INTO foo (val) VALUES
(null),(1),(null),(2),(null),(3),(null),(4),(null),(5),(null),(6),(null);

CREATE TABLE bar
(
  id    INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  val   INT
);

INSERT INTO bar (val) VALUES
(1),(2),(3),(4),(5),(6);

CREATE TABLE baz
(
  id    INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  val   INT
);

INSERT INTO baz (val) VALUES
(null),(null),(null),(null),(null),(null);

Query 1:

SELECT  'foo'                   as table_name,
        'mixed null/non-null'   as description,
        21                      as expected_sum,
        COALESCE(SUM(val), 0)   as actual_sum
FROM    foo
UNION ALL

SELECT  'bar'                   as table_name,
        'all non-null'          as description,
        21                      as expected_sum,
        COALESCE(SUM(val), 0)   as actual_sum
FROM    bar
UNION ALL

SELECT  'baz'                   as table_name,
        'all null'              as description,
        0                       as expected_sum,
        COALESCE(SUM(val), 0)   as actual_sum
FROM    baz

Results:

| TABLE_NAME |         DESCRIPTION | EXPECTED_SUM | ACTUAL_SUM |
|------------|---------------------|--------------|------------|
|        foo | mixed null/non-null |           21 |         21 |
|        bar |        all non-null |           21 |         21 |
|        baz |            all null |            0 |          0 |
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • 2
    Isn't SELECT SUM(IFNULL(column,0)) FROM table GROUP BY something more correct? What if we have some IS NULL values and some real values in "column"? – DarkSide Nov 11 '13 at 15:03
  • 2
    @DarkSide: [Absolutely nothing](http://www.sqlfiddle.com/#!2/ad735/1/0) unexpected. – Brad Christie Jun 11 '14 at 18:35
  • @BradChristie yes you're absolutely right. SUM works fine with NULL values too. – DarkSide Jun 11 '14 at 21:06
  • 1
    Please be aware that while`SUM` does function as desired, `AVG` and `COUNT` can [produce different results](http://www.sqlfiddle.com/#!9/09dcb9/15) when receiving `NULL` Prompting you to use `COALESCE` as suggested by @DarkSide, depending on your desired outcome. – Will B. Mar 08 '19 at 17:58
  • @fyrye depends on what you're after, absolutely. But so will any function that relies on a count of values over a broad summation. – Brad Christie Mar 08 '19 at 18:07
82

Use IFNULL or COALESCE:

SELECT IFNULL(SUM(Column1), 0) AS total FROM...

SELECT COALESCE(SUM(Column1), 0) AS total FROM...

The difference between them is that IFNULL is a MySQL extension that takes two arguments, and COALESCE is a standard SQL function that can take one or more arguments. When you only have two arguments using IFNULL is slightly faster, though here the difference is insignificant since it is only called once.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
6

Can't get exactly what you are asking but if you are using an aggregate SUM function which implies that you are grouping the table.

The query goes for MYSQL like this

Select IFNULL(SUM(COLUMN1),0) as total from mytable group by condition
Flexo
  • 87,323
  • 22
  • 191
  • 272
Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
0

if sum of column is 0 then display empty

select if(sum(column)>0,sum(column),'')
from table 
Sreejith N
  • 25
  • 5