Skip to content
Advertisement

SQL: SUM on Aggregate columns

I have this query:

Which has this result:

However, I’d like to add order_total and item_total in order to get just total.

So I’d expect this:

Doing this did not work:

Another monkeywrench is that the type of the numbers is money not integer.

Anyone would be able to help?

Advertisement

Answer

If any of the 2 sums returns NULL then the result of the sum of the sums will also be NULL because NULL + anything returns NULL.

Use SUM() only once:

If any of the columns involved may also be NULL use COALESCE() like COALESCE(orders.shipping_cost, 0::money)

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