sunbin1113 发表于 2007-10-31 22:19

求一更新表的方法

现有一张表,内容大致如下:

id       amount            version
1      50                     0
1      150                     1
1      200                     2
2      20                     1
2      50                     2
3      100                     0
3      200                     1
3      400                     2
3      800                     3

想更新成:

id       amount            version
1      1                         0
1      51                     1
1      151                     2
2      1                         1
2      21                     2
3      1                         0
3      101                     1
3      201                     2
3      401                     3

针对每一个分类(id),会有不同的version,以及相应的amount。现在想要根据对每一个分类,找到其version最小的一行,将其amount设为1;当version为第二小的,将amount设为version最小时的amount加1,以此类推。

希望大家各抒己见,看看有没有好的办法实现。

xxyyy 发表于 2007-11-01 09:12

用存储过程

sunbin1113 发表于 2007-11-01 09:16

能不能给点提示,我不会写informix的存贮过程。

xxyyy 发表于 2007-11-01 14:49

原帖由 sunbin1113 于 2007-11-1 09:16 发表 http://bbs.chinaunix.net/images/common/back.gif
能不能给点提示,我不会写informix的存贮过程。

这个应该查阅相关书籍,不用存储过程可以用编程序的方法,比如esql/c等,但要用游标。

sunbin1113 发表于 2007-11-01 14:58

我自己写一个过程,但是运行不了,麻烦看看:

create procedure upstaf()
define v_id integer;
define v_s integer;
define v_m integer;

begin work;

foreach cur_id for select staffnr into v_id from staf where firma=1 group by staffnr;
select min(seilands),max(seilands) into v_s,v_m from staf where firma=1 and staffnr=v_id;
for i in v_s to v_m
if i=v_s then
insert into my_staff values(v_id,1,i);
amount_last=select amount from staf where firma=1 and seiland=i and staffnr=v_id;
else
insert into my_staff values(v_id,amount_last+1,i);
amount_last=select amount from staf where firma=1 and seiland=i-1 and staffnr=v_id;
end if;
end for;
end foreache;
commit work;
end procedure;
页: [1]
查看完整版本: 求一更新表的方法