查询更新行

时间:2011-06-28 05:45:37

标签: sql oracle11g

我有一张结构表:

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

当Country列中的值唯一时,我必须将DUPLICATE列更新为'Y',当值不唯一时,我必须将'N'更新为'N'。 我试图在

的帮助下完成这项工作
select Country,dupe_count
count(*) over (partition by Country) as dupe_count
from newTable 

此查询将返回国家/地区名称和DUP列(相应国家/地区字段的外观数字)。 但是无法做到。 任何想法如何做或有更好的方法来做到这一点。 请帮忙。

3 个答案:

答案 0 :(得分: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
  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 :(得分:0)

用oracle不太确定 - 自从我用它以来已经很久了。但是从内存来看,它与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
)

答案 2 :(得分:0)

在重复列中,当值不唯一时,您想要放置'N'。表示具有重复记录的列Country的值然后您想要放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;