下面是语句中case部分,当情况出现有很多的时候,发现查询时间需要非常久,请教下大家还有没有其他更好的方法处理,另外建表还是其他???谢谢。case 
when (AA-BB >0 and AA-BB <0.01) then '>0          <0.01'
when (AA-BB >0.01 and AA-BB <0.02) then '>0.01 <0.02'
when (AA-BB >0.02 and AA-BB <0.03) then '>0.02 <0.03'
when (AA-BB >0.03 and AA-BB <0.04) then '>0.03 <0.04'
when (AA-BB >0.04 and AA-BB <0.05) then '>0.04 <0.05'
when (AA-BB >0.05 and AA-BB <0.06) then '>0.05 <0.06'
when (AA-BB >0.06 and AA-BB <0.07) then '>0.06 <0.07'
when (AA-BB >0.07 and AA-BB <0.08) then '>0.07 <0.08'
when (AA-BB >0.08 and AA-BB <0.09) then '>0.08 <0.09'
when (AA-BB >0.09 and AA-BB <0.10) then '>0.09 <0.10'
when (AA-BB >0.10 and AA-BB <0.11) then '>0.10 <0.11'
when (AA-BB >0.11 and AA-BB <0.12) then '>0.11 <0.12'
when (AA-BB >0.12 and AA-BB <0.13) then '>0.12 <0.13'
when (AA-BB >0.13 and AA-BB <0.14) then '>0.13 <0.14'
when (AA-BB >0.14 and AA-BB <0.15) then '>0.14 <0.15'
when (AA-BB >0.15 and AA-BB <0.16) then '>0.15 <0.16'
when (AA-BB >0.16 and AA-BB <0.17) then '>0.16 <0.17'
when (AA-BB >0.17 and AA-BB <0.18) then '>0.17 <0.18'
when (AA-BB >0.18 and AA-BB <0.19) then '>0.18 <0.19'
when (AA-BB >0.19 and AA-BB <0.20) then '>0.19 <0.20'
when (AA-BB >0.20 and AA-BB <0.21) then '>0.20 <0.21'
when (AA-BB >0.21 and AA-BB <0.22) then '>0.21 <0.22'
when (AA-BB >0.22 and AA-BB <0.23) then '>0.22 <0.23'
when (AA-BB >0.23 and AA-BB <0.24) then '>0.23 <0.24'
when (AA-BB >0.24 and AA-BB <0.25) then '>0.24 <0.25'
when (AA-BB >0.25 and AA-BB <0.26) then '>0.25 <0.26'
when (AA-BB >0.26 and AA-BB <0.27) then '>0.26 <0.27'
when (AA-BB >0.27 and AA-BB <0.28) then '>0.27 <0.28'
when (AA-BB >0.28 and AA-BB <0.29) then '>0.28 <0.29'
when (AA-BB >0.29 and AA-BB <0.30) then '>0.29 <0.30'
when (AA-BB >0.30 and AA-BB <0.31) then '>0.30 <0.31'........when AA-BB =0 then '=0'
when AA-BB =0.01 then '=0.01'
when AA-BB =0.02 then '=0.02'
when AA-BB =0.03 then '=0.03'
when AA-BB =0.04 then '=0.04'
when AA-BB =0.05 then '=0.05'
when AA-BB =0.06 then '=0.06'
when AA-BB =0.07 then '=0.07'
when AA-BB =0.08 then '=0.08'
when AA-BB =0.09 then '=0.09'
when AA-BB =0.10 then '=0.10'
when AA-BB =0.11 then '=0.11'
when AA-BB =0.12 then '=0.12'
when AA-BB =0.13 then '=0.13' ............
when (AA-BB >1 and AA-BB <1.1) then '>1 <1.1'
when (AA-BB >1.1 and AA-BB <1.2) then '>1.1 <1.2'
when (AA-BB >1.2 and AA-BB <1.3) then '>1.2 <1.3'
when (AA-BB >1.3 and AA-BB <1.4) then '>1.3 <1.4'
when (AA-BB >1.4 and AA-BB <1.5) then '>1.4 <1.5'
when (AA-BB >1.5 and AA-BB <1.6) then '>1.5 <1.6'
when (AA-BB >1.6 and AA-BB <1.7) then '>1.6 <1.7'
when (AA-BB >1.7 and AA-BB <1.8) then '>1.7 <1.8'
when (AA-BB >1.8 and AA-BB <1.9) then '>1.8 <1.9'
when (AA-BB >1.9 and AA-BB <2) then '>1.9 <2'
when (AA-BB >2 and AA-BB <2.1) then '>2 <2.1'
when (AA-BB >2.1 and AA-BB <2.2) then '>2.1 <2.2'
when (AA-BB >2.2 and AA-BB <2.3) then '>2.2 <2.3'
when (AA-BB >2.3 and AA-BB <2.4) then '>2.3 <2.4'
when (AA-BB >2.4 and AA-BB <2.5) then '>2.4 <2.5'
when (AA-BB >2.5 and AA-BB <2.6) then '>2.5 <2.6'...........end

解决方案 »

  1.   

    create table cfg_level (gate numeric(10,4), descript varchar(32))
    insert into cfg_level values (0, '=0')
    insert into cfg_level values (0.01, '=0.01')
    insert into cfg_level values (0.02, '=0.02')
    insert into cfg_level values (0.03, '=0.03')
    insert into cfg_level values (0.04, '=0.04')
    ...
    select
      ..
      isnull('='+cast(b0.gate as varchar),
             '>'+cast(b1.gate as varchar)
               +isnull('<'+cast(b2.gate as varchar),'')
        )
      ..
    from tb a
    join cfg_level b0 on b0.gate=(AA-BB)
    left join cfg_level b1 on b1.gate<(AA-BB) 
      and not exists (select 1 from cfg_level bb
        where bb.gate<(AA-BB) and bb.gate>b1.gate)
    left join cfg_level b2 on b2.gate>(AA-BB) 
      and not exists (select 1 from cfg_level bb
        where bb.gate>(AA-BB) and bb.gate<b1.gate)
      

  2.   

    引用 1 楼 dawugui 的回复:
    把这些参数做到一个表中,考虑用连接来做.
    这些参数可以做到一个表,但是本身判断这些情况的时候就需要用case when,该如何办捏
      

  3.   


    --参数表
    参数区间起点|参数区间终点
    select *
    from your_table a
         ,参数表 b
    where AA - BB between b.参数区间起点 and b.参数区间终点