0

I have been trying to find the correct way to sum 2 rows from 2 different tables.

I can easily identify the rows I want to sum using these two queries:

select * 
from   vui_trading_review_form_client 
where  month = '201202' and client_id='TOTALS';

+--------+------------+-----------+-----------------+-----------------+-----------------+----------------+----------+
| month  | dt_end     | client_id | amt_balance_GBP | amt_balance_EUR | amt_balance_USD | dt_working_day | order_no |
+--------+------------+-----------+-----------------+-----------------+-----------------+----------------+----------+
| 201202 | 2012-02-29 | TOTALS    |            2    |             3   |               4 | 2012-02-29     |        2 |
+--------+------------+-----------+-----------------+-----------------+-----------------+----------------+----------+


select * 
from   vui_trading_review_form_bank 
where  month = '201202' and provider='TOTALS';

+--------+------------+----------+-----------------+-----------------+-----------------+----------+
| month  | dt_end     | provider | amt_balance_GBP | amt_balance_EUR | amt_balance_USD | order_no |
+--------+------------+----------+-----------------+-----------------+-----------------+----------+
| 201202 | 2012-02-29 | TOTALS   |              1  |               1  |              1 |        3 |
+--------+------------+----------+-----------------+-----------------+-----------------+----------+

What I would like to achieve is a table which looks as follows

+-----------------+-----------------+-----------------+
| amt_balance_GBP | amt_balance_EUR | amt_balance_USD |
+-----------------+-----------------+-----------------+
| 1               |          2      |             3   |
+-----------------+-----------------+-----------------+

the first 3 totals minus the second three totals.

I have tried a join but i'm really struggling to get the right result.

Any suggestions would be greatly appreciated.

5
  • Your two queries are from the same table. I assume that's a mistake? Also, are you adding or subtracting those values (looks like subtracting)? Commented May 24, 2012 at 23:23
  • Hi John, sorry yes that is a mistake. I'm subtracting Commented May 24, 2012 at 23:31
  • the name of the two tables are the same? vui_trading_review_form_client? Commented May 24, 2012 at 23:45
  • sorry this is a mistake the second table is vui_trading_review_form_bank Commented May 24, 2012 at 23:48
  • ah ok, check my answer below. you need to use INNER JOIN or LEFT JOIN Commented May 24, 2012 at 23:50

2 Answers 2

1

Try this:

SELECT  (a.amt_balance_GBP - b.amt_balance_GBP) Total_GBP,
        (a.amt_balance_EUR - b.amt_balance_EUR) Total_EUR,
        (a.amt_balance_USD - b.amt_balance_USD) Total_USD
FROM vui_trading_review_form_client a INNER JOIN vui_trading_review_form_bank b
        on (a.Client_ID = b. provider) AND
           (a.Month = b.Month)
WHERE a.month = '201202' and b.provider='TOTALS';

or if it does not fit your needs, try to use LEFT JOIN

SELECT  (a.amt_balance_GBP - COALESCE(b.amt_balance_GBP,0)) Total_GBP,
        (a.amt_balance_EUR - COALESCE(b.amt_balance_EUR,0)) Total_EUR,
        (a.amt_balance_USD - COALESCE(b.amt_balance_USD,0)) Total_USD
FROM vui_trading_review_form_client a LEFT JOIN vui_trading_review_form_bank b
        on (a.Client_ID = b. provider) AND
           (a.Month = b.Month)
WHERE a.month = '201202' and b.provider='TOTALS';
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, this almost works. It ignores the where clause though where month = '201202' and provider='TOTALS'; when I try and add it I get an ambiguous result
yes because you need to specify on where the table of the column should be assigned. try to add the alias of the table so you won;t get ambiguous column error.
Okay thank you, when I run the query I get 3 warnings and the table produced gives all three values as 0. what does the 0 mean next to b.amt_balance_GBP?
do you mean COALESCE()? it a function that converts null into valid value eg. 0.
1

Are you just trying to do this:

select (a.amt_balance_GBP  - b.amt_balance_GBP) as GBP, 
   (a.amt_balance_EUR - b.amt_balance_EUR) as EUR,
   (a.amt_balance_USD - b.amt_balance_USD) as USD
from  vui_trading_review_form_client a, vui_trading_review_form_bank b
where a.month = '201202' and a.client_id ='TOTALS' and
      a.month = b.month and a.client_id = b.provider

3 Comments

Thank you for this response, yes this looks what I am trying to do. However when I run this I get a syntax error?
I had a typo in there, this should work in Sybase ASE or SQL Server im not sure about others though.
Thank you, again I get the same problem as with @johntotetwoo suggestion - all the sums equal 0. perhaps it is because I am using mysql

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.