最终结果是SELECT A,B,C FROM 表 
其中当A>100时,B=A*10 
A<100时B=A*20 
当B-A*0.1>1200时C=(B-A*0.1)*0.2 
<1200时C=(B-A*0.1)*0.3 
这样应该怎么写呢? 
因为C的值判断要写2遍CASE,而且有与B取值重复了,如果单独用C=(B-A*0.1)*0.3,SQL报错,无法找到B的字段

解决方案 »

  1.   

    上面可以简化:
    60.3<A<100,B=A*20,B-A*0.1 >1200成立,C=3.98A,(239.99,398)
    100<A<121.2,B=A*10,B-A*0.1 <1200成立,C=2.97A,(297,359.96)
    显然,B值不在C的区间.
    其他的不知道楼主要求什么,只能简化一下计算列.
      

  2.   

    --用子查询select 
            A, 
            B, 
            case 
                    when B-A*0.1 >1200 then (B-A*0.1)*0.2
                    when B-A*0.1 <1200 then (B-A*0.1)*0.3
            end as C
    from 
        (
            select 
                    A,
                    case when A>100 then A*10 when A<100 then A*20 end as B
            from 表名
        ) as T
      

  3.   

    应该存在先后的问题,楼主是想先给B赋值呢,还是想先给C赋值.
    declare @t table(a int,b int,c float)
    insert into @t
    select 30,56,78
    union all select 111,34,64
    union all select 500,3,30
    union all select 120,3,90
    union all select  60,34,64
    --select * from @t
    update @t set b=case when a>100 then a*10
                         when a<100 then a*20
                    end,
    c=case when (b-a/10)>1200 then (b-a/10)*2/10 --先给C赋值后再改B
            when (b-a/10)<1200 then (b-a/10)*3/10
       endselect * from @tdeclare @t table(a int,b int,c float)
    insert into @t
    select 30,56,78
    union all select 111,34,64
    union all select 500,3,30
    union all select 120,3,90
    union all select  60,34,64
    --select * from @t
    update @t set b=case when a>100 then a*10
                         when a<100 then a*20
                    end
    update @t set c=case when (b-a/10)>1200 then (b-a/10)*2/10--用两个语句先改B后再改C
                          when (b-a/10)<1200 then (b-a/10)*3/10
                     end
    select * from @t