2010-01-18 107 views
0

我需要一個更新查詢在同一組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   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 
+12

好,我建議你寫一個!當你發現它不起作用時,將它張貼在這裏,有人會毫無疑問地幫助你。 SO不是人們爲你寫免費代碼的地方。 – 2010-01-18 07:13:19

回答

0
-- 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 
1

的數據庫服務器與另一個值設置爲空值是你使用?在Oracle中,這可能是工作,assumming filed3是集團或空相等的每一行:

 
[email protected] > 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 

[email protected] > 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; 

[email protected] > 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