sql 根据字段大于或小于零使用不同的计算公式

qq_15637797 2016-05-27 01:41:00
字段 A 字段B 公式
>0 >0 A/B
<0 >0 1-A/B
>0 <0 A/B-1
<0 <0 2-A/B




...全文
1522 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghx287524027 2016-05-27
  • 打赏
  • 举报
回复
引用 4 楼 wmxcn2000 的回复:
估计这些条件,可能随时在变,现在是 4 个条件,没准儿哪天就成 5 个了。这个要用动态语句了;
所以还是问清楚具体想做什么吧,什么样的环境下
蓝色蛋饼 2016-05-27
  • 打赏
  • 举报
回复
select A,B,
(case when a>0 and b>0 then a/b
      when a<0 and b>0 then 1-a/b
      when a>0 and b<0 then a/b-1
      when a<0 and b<0 then 2-a/b end) C,
  (case when a>0 and b>0 then 'a/b'
      when a<0 and b>0 then '1-a/b'
      when a>0 and b<0 then 'a/b-1'
      when a<0 and b<0 then '2-a/b'end) D
 from tab;
卖水果的net 2016-05-27
  • 打赏
  • 举报
回复
估计这些条件,可能随时在变,现在是 4 个条件,没准儿哪天就成 5 个了。这个要用动态语句了;
xu176032 2016-05-27
  • 打赏
  • 举报
回复

select case when sign(A) =1 and sign(B)=1 then A/B
        when sign(A) =-1 and sign(B)=1 then 1-A/B
        when sign(A) =1 and sign(B)=-1 then A/B-1
        when sign(A) =-1 and sign(B)=-1 then 2-A/B end  from tab
大概这样,试试
ghx287524027 2016-05-27
  • 打赏
  • 举报
回复
具体是什么需求啊?
原味紅豆 2016-05-27
  • 打赏
  • 举报
回复
在表上建一个触发器,在插入或修改数据时自动更新公式,算出结果 create or replace TRIGGER adsa before insert or update ON 表名 FOR EACH ROW begin if :new.字段 A>0 then if :new.字段 B>0 then :new.公式:=:new.字段 A/:new.字段 B; else :new.公式:=:new.字段 A/:new.字段 B-1; end if; else if :new.字段 B>0 then :new.公式:=1-:new.字段 A/:new.字段 B; else :new.公式:=2-:new.字段 A/:new.字段 B-1; end if; end if; end;

17,382

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧