字段:
id     题目     问题     解决     考核     得分  
1       5       6       5       3
2       6       5       4       3
3       7       5       5       5  要求:
得分1=如果问题>=题目则得40,如果问题<题目则得问题/题目×40
得分2=如果解决>=题目则得30,如果解决<题目则得解决/题目×30
得分3=如果考核>=题目则得30,如果考核<题目则得考核/题目×30得分=得分1+得分2+得分3结果为:id     题目     问题     解决     考核     得分  
1       5       6       5       3       88
2       6       5       4       3       65.33
3       8       5       5       5      62.5

解决方案 »

  1.   

    create table tb (id int, 题目 int, 问题 int, 解决 int, 考核 int, 得分 int) 
    insert into tb values(1 , 5 , 6 , 5 , 3 , 0)
    insert into tb values(2 , 6 , 5 , 4 , 3 , 0)
    insert into tb values(3 , 7 , 5 , 5 , 5 , 0)
    goselect * ,
      case when 问题 >= 题目 then 40 else 问题*1.0/题目 * 40 end + 
      case when 解决 >= 题目 then 30 else 解决*1.0/题目 * 30 end + 
      case when 考核 >= 题目 then 30 else 考核*1.0/题目 * 30 end  from tbdrop table tb /*
    id          题目          问题          解决          考核          得分                                          
    ----------- ----------- ----------- ----------- ----------- ----------- ------------------------------- 
    1           5           6           5           3           0           88.000000000000
    2           6           5           4           3           0           68.333333333300
    3           7           5           5           5           0           71.428571428500(所影响的行数为 3 行)
    */
      

  2.   

    不知道我算错了,还是你给的结果错了?create table tb (id int, 题目 int, 问题 int, 解决 int, 考核 int, 得分 int) 
    insert into tb values(1 , 5 , 6 , 5 , 3 , 0)
    insert into tb values(2 , 6 , 5 , 4 , 3 , 0)
    insert into tb values(3 , 7 , 5 , 5 , 5 , 0)
    goselect id , 题目 , 问题 , 解决 , 考核,
      cast(case when 问题 >= 题目 then 40 else 问题*1.0/题目 * 40 end + 
      case when 解决 >= 题目 then 30 else 解决*1.0/题目 * 30 end + 
      case when 考核 >= 题目 then 30 else 考核*1.0/题目 * 30 end as decimal(18,2)) 得分from tbdrop table tb /*
    id          题目          问题          解决          考核          得分                   
    ----------- ----------- ----------- ----------- ----------- -------------------- 
    1           5           6           5           3           88.00
    2           6           5           4           3           68.33
    3           7           5           5           5           71.43(所影响的行数为 3 行)
    */
      

  3.   


    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([id] int,[题目] int,[问题] int,[解决] int,[考核] int,[得分] dec(18,2))
    insert [tb]
    select 1,5,6,5,3,null union all
    select 2,6,5,4,3,null union all
    select 3,7,5,5,5,null
     
    ---查询---
    select 
      id,
      题目,
      问题,
      解决,
      考核,
      得分=cast((case 
              when 问题>=题目 then 40  
              else 问题*1.0/题目*40 
            end) 
          +(case 
              when 解决>=题目 then 30
              else 解决*1.0/题目*30
            end)
          +(case
              when 考核>=题目 then 30
              else 考核*1.0/题目*30
            end) as dec(18,2))      
    from [tb]
    ---结果---
    id          题目          问题          解决          考核          得分                   
    ----------- ----------- ----------- ----------- ----------- -------------------- 
    1           5           6           5           3           88.00
    2           6           5           4           3           68.33
    3           7           5           5           5           71.43(所影响的行数为 3 行)
      

  4.   

    谢谢各位了。
    自己写时总是得null,才发现任何+null都等于null。