当前位置: 代码迷 >> Oracle开发 >> update多条记录!解决方案
  详细解决方案

update多条记录!解决方案

热度:64   发布时间:2016-04-24 07:57:43.0
update多条记录!
有个表:
    id       sort     value  
      1           1          
      1           2    
      1           3
      2           1
      2           2
      2           3
      2           4
      2           5
    ...       ...
想把每个id中sort最小的,也就是1,update   value   =   1,最大的,update   value=2,其余的update   value   =   4,能用一句sql写出来吗?


------解决方案--------------------
update test set value=(select decode(test.sort - a.min_s,0,1,decode(test.sort - a.max_s,0,2,4)) from (select id,min(sort) min_s,max(sort) max_s from test group by id) a where a.id=test.id)
------解决方案--------------------
create table test(id number,sort number,value number)

insert into test(id,sort) values(1,1);
insert into test(id,sort) values(1,2);
insert into test(id,sort) values(1,3);
insert into test(id,sort) values(2,1);
insert into test(id,sort) values(2,2);
insert into test(id,sort) values(2,3);
insert into test(id,sort) values(2,4);
insert into test(id,sort) values(2,5);
commit;

select * from test

update test a
set value = (select decode(b.sort, c.max, 2, c.min, 1, 4)
from test b,
(select max(sort) max, min(sort) min from test) c
where b.id = a.id
and b.sort = a.sort)
  相关解决方案