sql更新查询
我需要一个更新查询来设置空值和同一组中的另一个值 比如说sql更新查询,sql,Sql,我需要一个更新查询来设置空值和同一组中的另一个值 比如说 Table filed1 filed2 filed3 1 e 1/1/2009 1 e null 1 e 1/1/2009 2 f 2/2/2009 2 f 2/2/2009 2 f
Table
filed1 filed2 filed3
1 e 1/1/2009
1 e null
1 e 1/1/2009
2 f 2/2/2009
2 f 2/2/2009
2 f null
3 g 3/3/2009
3 g null
预期结果
filed1 filed2 filed3
1 e 1/1/2009
1 e 1/1/2009
1 e 1/1/2009
2 f 2/2/2009
2 f 2/2/2009
2 f 2/2/2009
3 g 3/3/2009
3 g 3/3/2009
您正在使用哪个数据库服务器?在Oracle中,这可能会起作用,假设组中的每一行的汇总filed3相等或为空: EMPI@XE > select * from sov; FILED1 F FILED3 1 e 1 e 1/1/2009 1 e 1/1/2009 2 g 1/2/2009 2 g 1/2/2009 2 g EMPI@XE > update sov s 2 set 3 s.filed3 = 4 (select filed3 5 from sov so 6 where so.filed1 = s.filed1 7 and so.filed2 = s.filed2 8 and so.filed3 is not null 9 and rownum = 1) 10 where filed3 is null; EMPI@XE > select * from sov; FILED1 F FILED3 1 e 1/1/2009 1 e 1/1/2009 1 e 1/1/2009 2 g 1/2/2009 2 g 1/2/2009 2 g 1/2/2009
好吧,我建议你写一本!当你发现它不起作用时,把它贴在这里,毫无疑问会有人帮你。因此,这里不是人们为您编写免费代码的地方。
-- note table1 is your table
UPDATE t1 set t1.filed3 = t2.filed3
from table1 t1, table1 t2
where t1.filed3 is null
and t2.filed3 is not null
and t1.filed2 = t2.filed2 and
t1.filed1 = t2.filed1