Oracle数据库计算的有关问题
下图为需求
以下为我自己写的两种方法,均报错,求大神解答~
方法1:
select t2.name,
t2.Money1,
t2.Money2,
t2.Money1 - t2.Money2 as poor,
(Round((t2.Money1 - t2.Money2) / t2.Money1, 2)) as poor2
from (select t.name,
(case t.type
when '1' then
sum(t.money)
end) as Money1,
(case t.type
when '2' then
sum(t.money)
end) as Money2
from test2 t) t2
group by t2.name
报错内容:ORA-00937:不是单组分组函数
方法2:
select t2.name,
t2.Money1,
t2.Money2,
t2.Money1 - t2.Money2 as poor,
(Round((t2.Money1 - t2.Money2) / t2.Money1, 2)) as poor2
from(select t.name,
(if t.type=1 then sum(t.money) end if) as Money1,
(if t.type=2 then sum(t.money) end if) as Money2
from test2 t)t2
group by t2.name
报错内容:ORA-00917:缺失右括号
------解决方案--------------------
你group by写的位置不对啊,group by应该写在子查询里面吧
------解决方案--------------------
select t1.name Name, t1.m1 Money1, t2.m2 Money2, t1.m1-t2.m2 Poor, (t1.m1-t2.m2)/t1.m1 Poor2
from
(select name, sum(money1) m1
from tablename
where type = '1'
group by name) t1,
(select name, sum(money2) m2
from tablename
where type = '2'
group by name) t2
where t1.name = t2.name