0

2 つの異なるテーブルから 2 つの行を合計する正しい方法を見つけようとしています。

次の 2 つのクエリを使用して、合計する行を簡単に特定できます。

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 |
+--------+------------+----------+-----------------+-----------------+-----------------+----------+

私が達成したいのは、次のようなテーブルです

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

最初の 3 つの合計から次の 3 つの合計を引いたもの。

参加を試みましたが、正しい結果を得るのに本当に苦労しています。

どんな提案でも大歓迎です。

4

2 に答える 2

1

これを試して:

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';

または、ニーズに合わない場合は、使用してみてください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';
于 2012-05-24T23:49:01.563 に答える
1

これをやろうとしているだけですか:

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
于 2012-05-24T23:50:52.167 に答える