select name,sum( case when flag2=1 then num else 0 end) as count1,
sum(case when flag2=2 then num else 0 end) as count2
from table2
group by name;

解决方案 »

  1.   

    to  masterz(www.fruitfruit.com)我知道怎么写select语句,我是不知道怎么写PROCEDURE,能不能写个完整的PROCEDURE,谢谢!
      

  2.   

    CREATE OR REPLACE PROCEDURE count_proc AS
    BEGIN  
    insert into table3 select name,sum( case when flag2=1 then num else 0 end) as count1,sum(case when flag2=2 then num else 0 end) as count2 from table2 group by name;
    commit;
    END;
      

  3.   

    CREATE OR REPLACE PROCEDURE pro_test AS
    BEGIN
       insert into table2 select * from table1;
       insert into table3 select name,sum( case when flag2=1 then num else 0 end) as count1,sum(case when flag2=2 then num else 0 end) as count2 from table2 group by name;
       commit;
    END;