TB1
---------------------------------------
工艺代码          分界点数 损耗率
---------------------------------------
C001 工艺一 10000 10.00 0
C001 工艺一 5000 5.00 1
C001 工艺一 1000 1.00 2要求:
根据录入的一个数,同表TB1中的分界点数做比较,分段取损耗率
如数量为1000或小于1000 取1,数量在1001-5000 取5
数理在5001-10000 取10 ,如何来写这个SQL 。不用过程

解决方案 »

  1.   

    不是吗,我也想过,一句CASE WHEN 能搞定
      

  2.   

    select case when 分界点数 <=1000 then 1 
    when 分界点数 between 1001 and 5000 then 5
    when 分界点数 between 5001 and 10000 then 10 
    ...............
    from tb
      

  3.   


    --仅供参考
    create table #tb(sort1 int,sort2 float)
    insert into #tb 
    select '10000','10' union all
    select '4400','5' union all
    select '1000','1'  
    select * from #tbselect case when sort1 <=1000 then 1 
    when sort1 between 1001 and 5000 then 5
    when sort1 between 5001 and 10000 then 10 
    end
    from #tb
    drop table #tb
      

  4.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-07-24 21:44:40
    ----------------------------------------------------------------
    --> 测试数据:[tb1]
    if object_id('[tb1]') is not null drop table [tb1]
    create table [tb1]([工艺代码] varchar(4),[工艺] varchar(6),[分界点数] int,[损耗率] numeric(4,2),[col] int)
    insert [tb1]
    select 'C001','工艺一',10000,10.00,0 union all
    select 'C001','工艺一',5000,5.00,1 union all
    select 'C001','工艺一',1000,1.00,2
    --------------开始查询--------------------------
    declare @a int
    set @a=4000
    select 
    distinct 损耗率=
    (  case  when @a<=1000 then 1
             when @a>1000 and @a<=5000 then 5
             when @a>=5001 and @a<=10000 then 10 end)  
    from [tb1]
    ----------------结果----------------------------
    /*损耗率         
    ----------- 
    5(所影响的行数为 1 行)
    */
      

  5.   

    declare @tb table(code varchar(10),fjds int,shl decimal(18,2))insert @tb select 'C001' , 10000, 10.00 
    insert @tb select 'C001' , 5000, 5.00 
    insert @tb select 'C001' , 1000, 1.00 
    --先组合数量段
    --用临时表是为了让你看的更清楚。
    select code,fjds1=isnull((select top 1 fjds from @tb where fjds<a.fjds order by fjds desc),0),fjds2=fjds,shl into #
    from @tb a order by fjds1
    select * from #
    --你要录入的那个数
    declare @aa int
    set @aa=100select shl from # where @aa between fjds1 and fjds2
    drop table #/*
    code       fjds1       fjds2       shl                  
    ---------- ----------- ----------- -------------------- 
    C001       0           1000        1.00
    C001       1000        5000        5.00
    C001       5000        10000       10.00(所影响的行数为 3 行)
    *//*
    shl                  
    -------------------- 
    1.00(所影响的行数为 1 行)
    */
      

  6.   

    谢谢楼上的各位,不知是我描述不清,不是各位没有真正理解。
    就一个表,表中的分界点数是变化数的不固定的。上述的都要求case 后的条件值是固定的。所以不是没有达到想到的效果。
      

  7.   

    select top 1 损耗率 from tb 
    where 分界点数<=@input
    order by 分界点数 desc
      

  8.   

    case when 是一种简单直接而有效的方式,
    但如果阀值时常需要调整,那么以配置表的形式进行调整更为灵活可靠:create table cfg_value2level (
      categoryid int not null ,
      valuegate int not null ,
      level int not null ,
      primary key (categoryid, valuegate)
    )insert into cfg_value2level values (1, 1000, 1)
    insert into cfg_value2level values (1, 5000, 5)
    insert into cfg_value2level values (1,10000,10)select a.*, b.level
    from tb a
    join cfg_value2level b on b.categoryid=1 and a.value<=b.value
    where not exists (select 1 from cfg_value2level b0 
        where b0.categoryid=b.categoryid and a.value<=b0.value
        and b.value<b0.value)
      

  9.   


    --easy,这个问题没注意到。
    --modify
    declare @tb table(code varchar(10),fjds int,shl decimal(18,2))insert @tb select 'C001' , 10000, 10.00 
    insert @tb select 'C001' , 5000, 5.00 
    insert @tb select 'C001' , 1000, 1.00 
    --先组合数量段
    --用临时表是为了让你看的更清楚。
    select code,fjds1=isnull((select top 1 fjds from @tb where fjds<a.fjds order by fjds desc)+1,0),fjds2=fjds,shl into #
    from @tb a order by fjds1
    select * from #
    --你要录入的那个数
    declare @aa int
    set @aa=100select shl from # where @aa between fjds1 and fjds2
    drop table #/*code       fjds1       fjds2       shl                  
    ---------- ----------- ----------- -------------------- 
    C001       0           1000        1.00
    C001       1001        5000        5.00
    C001       5001        10000       10.00(所影响的行数为 3 行)shl                  
    -------------------- 
    1.00(所影响的行数为 1 行)
    */