John Alexander Betts
John Alexander Betts

Reputation: 5204

Adding the total of two SUM functions

How can I do to sum the result of this queries using postgresql?:

SELECT SUM(value) FROM credit_balance

SELECT SUM(value) FROM debit_balance

I have tried this but it doesn't work:

SELECT SUM(SELECT SUM(value) FROM credit_balance UNION ALL SELECT SUM(value) FROM debit_balance)

Upvotes: 0

Views: 60

Answers (2)

The Code
The Code

Reputation: 1309

You are missing the field name after sum. you can write your query using union all as following:

select sum(val_sum) from (SELECT SUM(value) as val_sum FROM credit_balance UNION ALL SELECT SUM(value) as val_sum FROM debit_balance) as united_table;

OR

select sum(value) from (SELECT value FROM credit_balance UNION ALL SELECT value FROM debit_balance) as united_table.

Upvotes: 0

SQLChao
SQLChao

Reputation: 7847

SELECT
  (SELECT SUM(value) FROM credit_balance) + (SELECT SUM(value) FROM debit_balance)

Upvotes: 3

Related Questions