当前位置: 代码迷 >> Oracle开发 >> plsql的写法
  详细解决方案

plsql的写法

热度:176   发布时间:2016-04-24 07:42:09.0
求一个plsql的写法
id money rate 
1 100 100/400
2 300 300/400
3 350 1-100/400-300/400

计算rate:除了最后一条记录外,rate=money/400,最后一条记录的rate=1-(前面所有的rate和)。

------解决方案--------------------
如果id是1,2,3顺序排列的话就这样取
select id,money,money/400 rate from t where id<=(select count(*)-1 from t)
union
select id,money,1-(select sum(rate) from (select id,money,money/400 rate from t where id<=(select count(*)-1 from t))
 ) from t
------解决方案--------------------
修改表中的值
update t a set rate=(select money/400 from t b where a.id=b.id)
update t set rate=(select 1-sum(rate) from t where rownum<(select count(id) from t)) where id=(select max(id) from t)
------解决方案--------------------
修改下:
探讨
如果id是1,2,3顺序排列的话就这样取
select id,money,money/400 rate from t where id <=(select count(*)-1 from t)
union
select max(id) id,money,1-(select sum(rate) from (select id,money,money/400 rate from t where id <=(select count(*)-1 from t)) rate
) from t
  相关解决方案