0

I have 2 tables: incomes & outgo both containing information about transactions. I'd like to run a report to show the overall flow of money based on the totals in the tables, I currently do this manually by using the following queries:

1) SELECT amount, SUM(amount) AS intotal FROM incomes
2) SELECT amount, SUM(amount) AS outtotal FROM outgo

as an example, lets say intotal is 500 and outtotal is 300

I'd like a single query that gets both summaries and subtracts the outtotal amount from the intotal amount with a result of 200 in this case. can anyone point me in the right direction or help me with syntax?

1
  • SELECT SUM(amount) FROM (SELECT amount FROM incomes UNION SELECT amount FROM outgo) x Commented Jul 30, 2015 at 17:33

2 Answers 2

1

Try

SELECT
    (SELECT amount, SUM(amount) AS intotal FROM incomes) AS a,
    (SELECT amount, SUM(id) AS outtotal FROM outgo) AS b

And use with a.intotal and b.outtotal

Or directly with

SELECT 
    ((SELECT SUM(amount)FROM incomes) - (SELECT SUM(amount)FROM outgo)) AS total
Sign up to request clarification or add additional context in comments.

Comments

1

How about this:

SELECT
   (SELECT SUM(amount) FROM incomes)
   -
   (SELECT SUM(amount) FROM outgo)

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.