模拟or 改写union数据变多情形

SQL> select * from test1;

   CUST_ID	FR_ID CI_BALANCE LN_BALANCE	MGR_ID
---------- ---------- ---------- ---------- ----------
	10	   11	       0	  0	   100
	10	   11	      12	 12	   101
	10	   40	      12	 13	   101
	10	   11	      12	 13	   100

SQL> select * from test2;

   USER_ID
----------
       101
       102
       103
       100


SQL>  select cust_id,fr_id,sum(ci_balance),sum(ln_balance) from test1
    where (mgr_id=100 or mgr_id in (select * from test2))
   group by  cust_id,fr_id  2    3  ;

   CUST_ID	FR_ID SUM(CI_BALANCE) SUM(LN_BALANCE)
---------- ---------- --------------- ---------------
	10	   40		   12		   13
	10	   11		   24		   25


SQL> select cust_id,fr_id,sum(ci_balance),sum(ln_balance) from test1
where (mgr_id=100)
group by  cust_id,fr_id
union
select cust_id,fr_id,sum(ci_balance),sum(ln_balance) from test1
where mgr_id in (select * from test2)
group by  cust_id,fr_id  2    3    4    5    6    7  ;

   CUST_ID	FR_ID SUM(CI_BALANCE) SUM(LN_BALANCE)
---------- ---------- --------------- ---------------
	10	   11		   12		   13
	10	   11		   24		   25
	10	   40		   12		   13






SQL> select cust_id,fr_id,sum(ci_balance),sum(ln_balance) from test1
where (mgr_id=100)
group by  cust_id,fr_id  2    3  ;

   CUST_ID	FR_ID SUM(CI_BALANCE) SUM(LN_BALANCE)
---------- ---------- --------------- ---------------
	10	   11		   12		   13

SQL> select cust_id,fr_id,sum(ci_balance),sum(ln_balance) from test1
where mgr_id in (select * from test2)
group by  cust_id,fr_id  2    3  ;

   CUST_ID	FR_ID SUM(CI_BALANCE) SUM(LN_BALANCE)
---------- ---------- --------------- ---------------
	10	   40		   12		   13
	10	   11		   24		   25

原文地址:https://www.cnblogs.com/hzcya1995/p/13352234.html