如何根据另一个 table 组摘要获取摘要?

How to get summary based on another table group summary?

业务规则:仅当 trans_code 分组的总计 (trans_amount) > 0 时,才获取客户的总计 cust_points。

对于客户 #1,date_code 级别(代码 10)的摘要 > 0,因此 cust_points 总计 = 70。

对于客户 #2,仅代码 20 组总数 > 0,因此总共只有 75 个 cust_points

这是我的查询:

 with customers as
 (select '1' as cust_id, 10 as date_code, 30 as cust_points from dual union all
  select '1' as cust_id, 10 as date_code, 40 as cust_points from dual union all
  select '1' as cust_id, 20 as date_code, 22 as cust_points from dual union all --These points should not total because trans_amount sum for code 20  is  less than 0
  select '1' as cust_id, 40 as date_code, 33 as cust_points from dual union all  -- These points should not total because there is not trans_amounts > 0 for date_code
  select '2' as cust_id, 10 as date_code, 20 as cust_points from dual union all
  select '2' as cust_id, 20 as date_code, 65 as cust_points from dual union all
  select '2' as cust_id, 20 as date_code, 10 as cust_points from dual 
  ),
 transactions_row as
 (
 select '1' as cust_id, '10' as trans_code, 10.00   as trans_amount from dual union all
 select '1' as cust_id, '20' as trans_code, -15.00   as trans_amount from dual union all
 select '1' as cust_id, '20' as trans_code, -20.00   as trans_amount from dual union all
 select '1' as cust_id, '20' as trans_code, -10.00  as trans_amount from dual union all
 select '1' as cust_id, '30' as trans_code, 30.00   as trans_amount from dual union all
 select '1' as cust_id, '20' as trans_code, -20.00   as trans_amount from dual union all
 select '2' as cust_id, '10' as trans_code, -50.00   as trans_amount from dual union all
 select '2' as cust_id, '20' as trans_code, 20.00   as trans_amount from dual
 )
 select cust_id,
        sum(cust_points)
 from customers 
 where cust_id in
( 
select cust_id 
       from (
 select cust_id, trans_code, sum(trans_amount) 
 from transactions_row
 group by cust_id, trans_code
 having sum(trans_amount) > 0 
 )
 )
 group by cust_id



Desired Results

 CUST_ID    CUST_POINTS 
  1         70  /* (30 because total trans_amount for tran_code(10) > 0  +
                    40 because total trans_amount for tran_code(10) > 0) */

   2        75  /* Do not include the 20 points because total trans_amt for 10 < 0 */

这是使用 exists 的一种方法:

 select cust_id,
        sum(cust_points)
 from customers c
 where exists (
     select 1
     from transactions_row tr 
     where tr.trans_code = c.date_code
       and tr.cust_id = c.cust_id
     group by tr.trans_code, tr.cust_id
     having sum(tr.trans_amount) > 0
 )
 group by cust_id