0

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)

2 Answers 2

3
SELECT
  (SELECT SUM(value) FROM credit_balance) + (SELECT SUM(value) FROM debit_balance)
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.