Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android "null" from SUM

Tags:

android

sqlite

I have a query that takes data from 2 tables. It works, but the two SUMs return NULL if there are no values. How can I get zero instead?

SELECT SUM(g.field1), 
       SUM(g.field2), 
       c._id, 
       c.field_1, 
       c.field_3 
  FROM Table1 g 
  JOIN Tabel2 c 
 WHERE ( c.field_1 = g.field5 )
like image 659
user3160725 Avatar asked Jul 01 '26 16:07

user3160725


1 Answers

The SUM functions returns NULL if there are no non-NULL values.

To get a return value of 0 even if there are no input values, just replace SUM with the TOTAL function:

SELECT TOTAL(g.field1), 
       TOTAL(g.field2), 
       ...
like image 166
CL. Avatar answered Jul 04 '26 05:07

CL.