TABLE  A : CLASS(班级)、COUNT(人数)、S
TABLE  B:  STUDENT(学生)、CLASS(所属班级)、S
TABLE C:  STUDENT(学生)、COME(出生地 分为 CZ城镇  NC农村 两种)、S
TABLE D: STUDENT(学生)、CS(计算S的参数)现在要计算学生的S,存入TABLE B中规则如下:
当学生所在的班级人数>10时,才对学生的S进行计算;
当学生的COME为CZ时,其S值为TABLE C中对应的S值;
当学生的COME为NC时,基S值为TABLE A中的S值 乘 TABLE D中的CS 得到的值

解决方案 »

  1.   

    update b set b.s=(select c.s from a,b,c where c.student=b.student and b.class=a.class and a.count>10 and c.come='CZ');
    update b set b.s=(select a.s*d.cs from a,b,c,d where d.student=c.student and c.student=b.student and b.class=a.class and a.count>10 and c.come='NC');
    commit;
    这三句可以实现,看起来比较直观,但效率差些,你看看不行我在用存储过程给你做
      

  2.   

    merge into b t1
    using (
    select b.student,b.class,
           case when c.come = 'CZ' then c.s else a.s * d.cs end s
    from a,b,c,d
    where c.student = d.student
      and c.student = b.student
      and b.class = a.class
    where a.count > 0
    ) t2
    on (t1.student = t2.student and t1.class = t2.class)
    when matched then 
      update set t1.s = t2.s
    when not matched then
      insert (t1.student,t1.class,t1.s)
      values (t2.student,t2.class,t2.s)
      

  3.   

    作为一个完整的业务   还是放到一个过程里去比较好
    要一个sql的话 用case when试试