0

我发现像这样重复的数据库记录:

select s.name, r.name Region, c.name Country
from supplier s
join region r on r.id = s.regionid
join region c on c.id = isnull(r.pid, r.id)
group by s.name, r.name, c.name
having count(s.name) >1 

什么是列出它们的最佳方式(所以如果两个重复,它将出现两次等等......)

4

2 回答 2

2

最简单的方法是从您的 Find-dups 查询创建一个内联查询并加入“without-a-group-by”查询。

select s.name, r.name Region, c.name Country
     from supplier s
     join region r on r.id = s.regionid
     join region c on c.id = isnull(r.pid, r.id)
     inner join (select s.name, r.name Region, c.name Country
                from supplier s
                join region r on r.id = s.regionid
                join region c on c.id = isnull(r.pid, r.id)
                group by s.name, r.name, c.name
                having count(s.name) >1 ) dups
     ON s.name = dups.name
        and r.name = dups.region
        and c.name = dups.country
于 2012-04-23T16:37:26.610 回答
2

我认为应该这样做:

with C as (
  select
    s.name,
    r.name Region,
    c.name Country,
    count(*) over (
      partition by s.name, r.name, c.name
    ) as ct
  from supplier s
  join region r on r.id = s.regionid
  join region c on c.id = isnull(r.pid, r.id)
)
  select
    name, Region, Country
  from C
  where ct > 1;
于 2012-04-23T16:42:35.140 回答