2012-02-02 50 views
1

我有一個表叫用戶提供下列:甲骨文更新表中的同文行

Name, Location, Active (all varchars). 
John Berlin OK 
Danny London OK 
Robert NY  OK 
John Berlin OK 
Danny Berlin OK 

我想更新Active列NOK對於多次出現的(兩個,三個或x更多用戶時間) - 他們有相同的名稱和位置(在表中相同的行)像這樣:

John Berlin NOK 
    Danny London OK 
    Robert NY  OK 
    John Berlin NOK 
    Danny Berlin OK 

我該怎麼做? 謝謝!

回答

2

在Oracle中,您可以使用:

UPDATE your_table 
    SET active = 'NOK' 
WHERE (name, location) IN (SELECT name, location 
           FROM your_table 
          GROUP BY name, location 
          HAVING COUNT(*) > 1) 
0

你需要做的是這樣的:

插入重複的值到一個#temp表

select t.Name, t.Location 
into #test 
from tablename t 
group by t.Name, t.Location 
having count(t.Name) > 1 

進行更新

update tablename 
set Active = 'NOK' 
from tablename t 
inner join #test t2 on t.Name = t2.Name and t.Location = t2.Location 

或者一步到位:

update tablename 
set Active = 'NOK' 
from tablename t 
inner join (select t.Name, t.Location 
      from tablename t 
      group by t.Name, t.Location 
      having count(t.Name) > 1) t2 on t.Name = t2.Name and t.Location = t2.Location