和结果乘以oracle

时间:2017-07-19 05:36:39

标签: sql oracle group-by sum

我有一个名为 b_balance 的视图,它返回以下记录:

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

和另一个名为 t_balance 的视图,它返回以下记录:

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 

现在问题出现了,当我执行我的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 ;

返回以下记录:

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 

表示结果乘以行数。 我检查了堆栈溢出,根据this回答没有发现任何错误。

那我的SQL有什么问题?

2 个答案:

答案 0 :(得分:1)

您应加入汇总结果(不汇总已连接的值)

SELECT TO_CHAR(to_date('20170801','yyyyMMdd'), 'MM/dd/yyyy') AS TRADE_DATE,
  t1.SECURITIES_CODE,
  t1.SIDE,
  t1.C_b_balance,
  t1.P_b_balance,
  t2.C_t_balance,
  t2.P_t_balance,
from (
    SELECT
      b.securities_code            AS SECURITIES_CODE,
      b.buy_sell_flag              AS SIDE,
      SUM(NVL(b.c_balance,0))      AS C_b_balance,
      SUM(NVL(b.p_balance,0))      AS P_b_balance,
    FROM b_balance b
    GROUP BY b.securities_code, b.buy_sell_flag ) t1 
left join (
    SELECT 
      t.securities_code            AS SECURITIES_CODE,
      t.buy_sell_flag              AS SIDE,
      SUM(NVL(t.c_balance,0))      AS C_t_balance,
      SSUM(NVL(t.p_balance,0))     AS P_t_balance,
    FROM t_balance t
    GROUP BY t.securities_code, t.buy_sell_flag
) on t1.securities_code = t2.securities_code and t1.buy_sell_flag  = t2.buy_sell_flag

答案 1 :(得分:0)

使用来自不同表的聚合时,在加入之前进行聚合:

select
  date '2017-08-01'                                 as trade_date,
  securities_code                                   as securities_code,
  buy_sell_flag                                     as side,
  nvl(t.sum_c_balance, 0)                           as c_t_balance,
  nvl(b.sum_c_balance, 0)                           as c_b_balance,
  nvl(t.sum_c_balance, 0) - nvl(b.sum_c_balance, 0) as c_difference,
  nvl(t.sum_p_balance, 0)                           as p_t_balance,
  nvl(b.sum_p_balance, 0)                           as p_b_balance,
  nvl(t.sum_p_balance, 0) - nvl(b.sum_c_balance, 0) as p_difference  
from
(
  select
    securities_code,
    buy_sell_flag,
    sum(c_balance) as sum_c_balance,
    sum(p_balance) as sum_p_balance
  from b_balance
  group by securities_code, buy_sell_flag
) b
full outer join
(
  select
    securities_code,
    buy_sell_flag,
    sum(c_balance) as sum_c_balance,
    sum(p_balance) as sum_p_balance
  from t_balance
  group by securities_code, buy_sell_flag
) t using (securities_code, buy_sell_flag)
order by securities_code, buy_sell_flag;