id为编号,a是选择题的答案,a的值在1-4之间select id,a from tableid   a1    1
2    3
3    2a=1时 得25分
a=2时 得20分
a=3时 得15分
a=4时 得10分能否直接得出以下列表信息,并且计算出他们的总分id   a   fenshu1    1   25
2    3   15
3    2   20总分 25+15+20=60

解决方案 »

  1.   


    id       a 1         1 
    2         3 
    3         2 
    ----------------select id,a, fenshu=sum(case when a=1 then 25
                                 when a=2 then 15
                                 when a=3 then 20
                                 else 10
                             end)
    from t
    group by id,a 
      

  2.   

    select id,case a when  1 then 25 
                     when  2 then 15
                     when  3 then 20
                     else 0 end
    from table
      

  3.   

    declare @t table(id int,a int)
    insert @t select 1,1 
    insert @t select 2,3 
    insert @t select 3,2 
    select   id,sum(case   a   when     1   then   25   
                                      when     2   then   15 
                                      when     3   then   20 
                                      else   0   end) 
    from   @t
    group by id
    with rollup/*id                      
    ----------- ----------- 
    1           25
    2           20
    3           15
    NULL        60(所影响的行数为 4 行)
    */
      

  4.   

    declare @t table(id int,a int)
    insert @t select 1,1 
    insert @t select 2,3 
    insert @t select 3,2 
    select   id = case when GROUPING(id) = 1 then '总分'
             else left(id,10) end,
           
             fenshu 
     =  sum(case   a   when     1   then   25   
                                      when     2   then   15 
                                      when     3   then   20 
                                      else   0   end) 
    from   @t
    group by id
    with rollup/*
    id                   fenshu      
    -------------------- ----------- 
    1                    25
    2                    20
    3                    15
    总分                   60
    (所影响的行数为 4 行)
    */
      

  5.   

    非常感谢happyflystone 
    无枪狙击手