如下有个表编号 比例 属性
01 2 0.2
02 8 0.4
03 19 0.1
04 3 0.9
 . . .
 . . .
 . . .然后有这么一个公式:当 "比例"在(0,4)之间 计算出 "指数"= 比例/2;
当 "比例"在(4,8)之间 计算出 "指数"= 2+(比例-4/2);
当 "比例"在(11,14)之间 计算出 "指数"= ....就这样下去 ,然后上面的表呢,我已经查出来了,现在就是要通过上面的公式,得到下表:编号 比例 属性 指数
01 2 0.2 n1
02 8 0.4 n2
03 19 0.1 n3
04 3 0.9 n4
 . . . .
 . . . .
 . . . .当然这个n1~n4这些就是依据公式计算出来的,请问高人这个怎么做,SQL咋写?

解决方案 »

  1.   

    这个你该用sql区问用case when then end可以解决你的问题
      

  2.   


    SELECT 
         编号 , 
         比例 ,
         属性 ,
        '指数' = 
        CASE 
            WHEN 比例 between 0 and 4 THEN '指数'=比例/2
            WHEN 比例 between 4 and 8 THEN '指数'=2+ (比例-4/2)
            ELSE '指数'=比例/2
        END 
    FROM tb
      

  3.   

    case 语句select when
    case (编号>=0 And 编号 <=4) then 比例/2
    ...
    else ... endFrom yourTab
      

  4.   

    您好,好像这一句有错,说等号附近有语法错误就这句 
     WHEN 比例 between 0 and 4 THEN '指数'=比例/2
      

  5.   

    SELECT 编号 , 比例 ,
    属性 ,(CASE 
    WHEN 比例 between 0 and 4 THEN 比例/2
    WHEN 比例 between 4 and 8 THEN 2+ (比例-4/2)
    ELSE '' end) as 指数
    from tb
      

  6.   

    总结一下 最后这样出来的select time as 时间,(length_total/length) as 拥堵比例,
    (length_total/length)*0.4 as VMT权重, 
    case when (length_total/length)*100 between 0 and 4 
        then (length_total/length)*100/2
     when (length_total/length)*100 between 4 and 8 
        then ((((length_total/length)*100)-4)/2)+2
     when (length_total/length)*100 between 8 and 11 
        then (((((length_total/length)*100)-4)*2)/3)+4
     when (length_total/length)*100 between 11 and 14 
        then (((((length_total/length)*100)-11)*2)/3)+6
     when (length_total/length)*100 between 14 and 24 
        then ((((length_total/length)*100)-14)/5)+8
     else  (length_total/length)*0+10
    end 指数
     from CLASS1320100906 order by time