开发者

How to write sqlite select statement for NULL records

I have a column which contains null values in some of the rows.

I want to do sum of the column values by writing a selec开发者_如何学Pythont statement in sqlite.

How do I write the statement so that it treats null values as 0.

My current sqlite statement: select sum(amount) from table1 gives error as it returns null.

Please help.


Accordgin to SQLite documentation:

The sum() and total() aggregate functions return sum of all non-NULL values in the group. If there are no non-NULL input rows then sum() returns NULL but total() returns 0.0. NULL is not normally a helpful result for the sum of no rows but the

So I guess you'd better use total() instead of sum() in case you expect that the result be 'non-null'.


You can use ifnull(x,y) or coalesce(x,y,z,...) function. Each of them return the first non-null value from the parameter list from left to right. ifnull has exactly two parameter whereas coalesce has at least two.

select sum(ifnull(amount, 0)) from table1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜