2011-06-28 158 views
0

我有一個表結構:查詢更新

Country | DUPLICATE 
India | 
Australia| 
India | 
USA  | 
Germany | 
Germany | 

我要更新DUPLICATEcolumn爲「Y」當國家列中的值是唯一的,以「N」當值不是唯一的。 我試着用的

select Country,dupe_count 
count(*) over (partition by Country) as dupe_count 
from newTable 

這個查詢的幫助來完成這將返回國名和DUP柱(conataing相應的國家領域出現數)。 但無法做到這一點。 任何想法如何做到這一點或有沒有更好的方法來做到這一點。 請幫忙。

回答

0

甲骨文不太確定 - 自從我用了很長時間。但從內存來看,它與mssql不一樣。

UPDATE newTable 
SET DUPLICATE = 'Y' 
WHERE Country IN (
    SELECT COUNT(Country) 
    FROM newTable 
    GROUP BY Country 
    HAVING Count(Country) > 1 
) 


UPDATE newTable 
SET DUPLICATE = 'N' 
WHERE Country IN (
    SELECT COUNT(Country) 
    FROM newTable 
    GROUP BY Country 
    HAVING Count(Country) = 1 
) 
1

具有以下測試數據...

create table tq84_country (
    country varchar2(10) , 
    duplicate char(1) check(duplicate in ('Y', 'N')) 
); 

insert into tq84_country (country) values ('India'); 
insert into tq84_country (country) values ('Australia'); 
insert into tq84_country (country) values ('India'); 
insert into tq84_country (country) values ('USA'); 
insert into tq84_country (country) values ('Germany'); 
insert into tq84_country (country) values ('Germany'); 

...這update語句應該做的:

update 
    tq84_country a 
set 
    duplicate = (
    select 
     case when 
     count(*) > 1 then 'Y' 
         else 'N' 
     end 
    from 
     tq84_country b 
    where 
     a.country = b.country 
); 

驗證:

select * from tq84_country; 
+1

UPDATE語句當然意味着更新所有行。 –

+0

ohhh我很抱歉RenéNyffenegger ...這是正確的:)對不起以前的評論.... 好主意更新表 –

0

在重複列你想在'N'時放置e值爲不是唯一..意味着列國家中有重複記錄的值,那麼你想放N(否)

你可以用下面的查詢任何方式輕鬆地執行此任務

update newTable 
set DUPLICATE = 
case 
when country in (select country from newTable group by country having count(*) > 1) then 
'N' -- if you got my previous point then you have to edit this line with 'Y' 
else 
'Y' 
end;