Skip to content
Advertisement

sql SUM: change null with 0

I have a query like this:

And another like this:

I want to select from those tables, with condition

But I also want to change the null record which appears with 0, I’ve tried COALESCE but it still doesn’t give me the right result.

Advertisement

Answer

In general you would first set NULL values to 0 and then SUM them:

Otherwise your queries have a few deficiencies such as field LIKE '%' which is a really inefficient way of writing field IS NOT NULL. The use of a scalar sub-query makes the overall query really difficult to read and the joins can probably be optimized if rewritten as a sub-query instead.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement