oracle条件求和问题,高分求解!!! 15
witht1as(select3420200202grp,10001mbr,11type,800owefromdualunionallselect3420200202gr...
with t1 as ( select 3420200202 grp,10001 mbr,11 type,800 owe from dual union all select 3420200202 grp,10001 mbr,12 type,1600 owe from dual union all select 3420200202 grp,10001 mbr,21 type,200 owe from dual union all select 3420200202 grp,10002 mbr,12 type,1500 owe from dual union all select 3420200202 grp,10002 mbr,21 type,1000 owe from dual union all select 3420200202 grp,10003 mbr,21 type,4000 owe from dual ) select * from t1; /br 虚拟群3420200202,存在成员10001和10002,先需要统计群的owe金额,条件:成员存在type为11时,求owe和时过滤type为21的值 以上实例中群3420200202的值为:800+1600+1500+1000+4000 成员10001:800+1600(过滤type为21的值)成员10002:1500+1000 成员10003:4000 如果用一条语句完成计算群3420200202的owe汇总值,忘指点一二,谢谢!
展开
1个回答
展开全部
用下面这个语句试试,我没有测试,凭空想象的。
select MBR,sum(owe) from table
where (MBR in ( select MBR from table where type=11) and type !=21) or (MBR not in ( select MBR from table where type=11)) group by MBR
还有一个办法(总的-type=11的MBR中type=21的)(子查询计算那里可能有点问题,因为只有一个GRP所以没有group by也可以的):
select sum(owe)-(select sum(owe) from table where MBR in ( select MBR from table where type=11) and type=21) from table
select MBR,sum(owe) from table
where (MBR in ( select MBR from table where type=11) and type !=21) or (MBR not in ( select MBR from table where type=11)) group by MBR
还有一个办法(总的-type=11的MBR中type=21的)(子查询计算那里可能有点问题,因为只有一个GRP所以没有group by也可以的):
select sum(owe)-(select sum(owe) from table where MBR in ( select MBR from table where type=11) and type=21) from table
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询