I have a view named b_balance which returns the following records:
SECURITIES_CODE BUY_SELL_FLAG C_BALANCE P_BALANCE
--------------- ------------- --------- ---------
10042 BUY 200 0
10042 BUY 500 0
10042 SELL 200 0
10042 BUY 0 5000
10042 SELL 0 2500
10043 BUY 300 0
10043 SELL 0 2500
and another view named as t_balance which returns the following records:
SECURITIES_CODE BUY_SELL_FLAG C_BALANCE P_BALANCE
--------------- ------------- --------- ---------
10042 BUY 0 5000
10043 BUY 300 0
10042 SELL 200 0
10042 SELL 0 2500
10043 SELL 0 2500
10042 BUY 200 0
10042 BUY 500 0
Now the problem occurs, when I execute my SQL
SELECT TO_CHAR(to_date('20170801','yyyyMMdd'), 'MM/dd/yyyy') AS TRADE_DATE,
b.securities_code AS SECURITIES_CODE,
b.buy_sell_flag AS SIDE,
SUM(NVL(t.c_balance,0)) AS C_t_balance,
SUM(NVL(b.c_balance,0)) AS C_b_balance,
SUM(NVL(t.c_balance,0)) - SUM(NVL(b.c_balance,0)) AS C_DIFFERENCE,
SUM(NVL(t.p_balance,0)) AS P_t_balance,
SUM(NVL(b.p_balance,0)) AS P_b_balance,
SUM(NVL(t.p_balance,0)) - SUM(NVL(b.p_balance,0)) AS P_DIFFERENCE
FROM b_balance b
FULL OUTER JOIN t_balance t
ON b.securities_code = t.securities_code
AND b.buy_sell_flag = t.buy_sell_flag
GROUP BY b.securities_code,
b.buy_sell_flag
ORDER BY SECURITIES_CODE,
SIDE ;
this returns the following records:
TRADE_DATE SECURITIES_CODE SIDE C_T_BALANCE C_B_BALANCE C_DIFFERENCE P_T_BALANCE P_B_BALANCE P_DIFFERENCE
---------- --------------- ---- ----------- ----------- ------------ ----------- ------------ ------------
08/01/2017 10042 BUY 2100 2100 0 15000 15000 0
08/01/2017 10042 SELL 400 400 0 5000 5000 0
08/01/2017 10043 BUY 300 300 0 0 0 0
08/01/2017 10043 SELL 0 0 0 2500 2500 0
that means the result is being multiplied by number of rows. I checked on Stack overflow and did't find anything wrong according to this answer.
So what is the wrong in my SQL?