关于 数据 求和 本帖最后由 lyvscf 于 2010-09-19 16:49:05 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select flag,sum(integral) as cnt from t where flag=1union allselect flag,sum(abs(integral))*-1 as cnt from t where falg in (2,5) with t1 as (select deocode(sign(integral),1,'A','B') type,integral from table)select type,sum(integral ) from t1 group by type 得到的是 Type sum(integral) A 400 我想得到的是 Type sum(integral) A 400 D -200 SQL> select * from tb_test; ID FLAG INTEGRAL-- ---- ----------- 1 0 200 1 0 200 1 2 -100 1 5 -100 SQL> SQL> with t1 as (select decode(sign(integral),1,'A','D') type,integral from tb_test) 2 select type,sum(integral ) from t1 group by type 3 ; TYPE SUM(INTEGRAL)---- -------------A 400D -200 SQL> 敢问这位高手 如果表中 的flag 又有其他的 类型的 该如何处理? 按自己情况写decode,decode相当于if语句. select 类型, sum(积分) 积分和(select decode(flag,0,'A',2,'D',5,'D') 类型,integral 积分from tb) agroup by 类型 oracle绑定变量 缺少等号 oracle 如何查表和存储过程的关联 如何知道某个表或者对象,主键的自动增长用的是sequence 求解:一个求和的 SQL 序列自动增长的问题,高手请进!! oracle中如何实现十进制和二进制的相互转换? connect by调优 作业删除不掉 触发器问题,急,在线等候 oracle触发器问题 触发器操作异构数据库 紧急 求助oracle 字符串列 拼接的问题
union all
select flag,sum(abs(integral))*-1 as cnt from t where falg in (2,5)
select type,sum(integral ) from t1 group by type
得到的是
Type sum(integral)
A 400
我想得到的是
Type sum(integral)
A 400
D -200
ID FLAG INTEGRAL
-- ---- -----------
1 0 200
1 0 200
1 2 -100
1 5 -100
SQL>
SQL> with t1 as (select decode(sign(integral),1,'A','D') type,integral from tb_test)
2 select type,sum(integral ) from t1 group by type
3 ;
TYPE SUM(INTEGRAL)
---- -------------
A 400
D -200
SQL>
(select decode(flag,0,'A',2,'D',5,'D') 类型,integral 积分
from tb) a
group by 类型