计算totalsum的值,并update该值
表如下:
studentid chinese math english    totalsum
  0001       80     70    60
  0002       40     50    80
  0001       20     100   40
  0003       10     100   30
  0001       50     70    80
 ....
统计0001和0002的所有记录三门学科成绩之和如果大于500
则totalsum=(chinese math english)*0.5
如果小于500
则totalsum=(chinese math english)*0.2

解决方案 »

  1.   

    Update 表
    Set totalsum = B.totalsum
    From  表 A
    Left JOIN 
    (Select CASE WHEN chinese+math+english>500 then (chinese+math+english)*0.5 ELSE (chinese+math+english)*0.2 End totalsum,studentid
    From 表 where studentid IN ('0001','0002')) B
    ON A.studentid=B.studentid
      

  2.   

    上面的执行出错:
    The column prefix 'B' does not match with a table name or alias name used in the query.
      

  3.   

    假設表名為 StudyGradeUPDATE StudyGrade
      SET totalsum = 
        CASE  WHEN (ISNULL( chinese,0) + ISNULL(math,0) + ISNULL(english,0))>500 
              THEN (ISNULL( chinese,0) + ISNULL(math,0) + ISNULL(english,0))*0.5
                 ELSE  (ISNULL( chinese,0) + ISNULL(math,0) + ISNULL(english,0))*0.2 End 
    WHERE  studentid in ('0001','0002')
      

  4.   

    update table_a set totalsum=
    case when studentid 
    in (select studentid from table_a  group by studentid having sum(chinese+math+english)>=500)
    then (chinese+math+english)*0.5
    else
    (chinese+math+english)*0.2
    end
    where studentid in ('0001,'0002')
      

  5.   

    ACCESS不能用case when...end
    要改成iif,但我改成:
    update test1 set totalsum=
    iif (studentid in (select studentid from test1  group by studentid having 
       sum (chinese+math+english)>=500),(chinese+math+english)*0.5,0)
    iif (studentid in (select studentid from test1  group by studentid having 
       sum(chinese+math+english)<500),(chinese+math+english)*0.2)
    where studentid in ('0001','0002')为何还是不行?
      

  6.   

    那你改成两句试试,比如
    update table_a set totalsum=(chinese+math+english)*0.5
    where studentid 
    in (select studentid from table_a  group by studentid having sum(chinese+math+english)>=500)
    and studentid in ('0001,'0002')
      

  7.   

    select case when (sum(chinaese)+sum(math)+sum(english))>500  then (sum(chinaese)+sum(math)+sum(english))*0.5 else (sum(chinaese)+sum(math)+sum(english))*0.2 end as totalsum一句话就可以了.试试吧.