SQL语言里怎么做下列计算操作呢?追加送分100

来源:百度知道 编辑:UC知道 时间:2024/06/18 05:31:25
SELECT A,B,C FROM 表
其中当A>100时,B=A*10
A<100时B=A*10
当B-A*0.1>1200时C=(B-A*0.1)*0.2
<1200时C=(B-A*0.1)*0.3
这样应该怎么写呢?
因为C的值判断要写2遍CASE,而且有与B取值重复了
输出的结果是类似:select A,B,C FROM....这样的语句啊
楼下的好象不符合逻辑啊,而且计算C时,B哪来的呢?直接写SELECT会提示没有B这个字段啊,因为B=A*10(上面写错了,A<100时B=A*20)

当A>或者<100时,B的取值公式不变(当A=100时你没描述,不知是否是少写了)

下边的:if B-A*0.1>1200 then
select C=(B-A*0.1)*0.2 from table ;
end if;

if B-A*0.1<1200 then
select C=(B-A*0.1)*0.3 from table ;
end if;

不知道为什么不可以=1200,是少写了么?

由于取值一样,合并连接就行了.
具体代码
select A, A*10 as BB ,(B-A*0.1)*0.2 as CC from table where B-A*0.1>1200 and A>100
union
select A, A*20 as BB ,C as (B-A*0.1)*0.2 from table where B-A*0.1>1200 and A<100
union
select A, A*10 as BB ,(B-A*0.1)*0.3 as CC from table where B-A*0.1<1200 and A>100
union
select A, A*20 as BB ,C as (B-A*0.1)*0.3 from table where B-A*0.1<1200 and A<100