从连接表查询中仅选择重复项

Selecting ONLY Duplicates from a joined tables query

我有以下查询,我试图连接两个与其 ID 匹配的表,以便我可以获得 "c.code" 中的重复值。我尝试了很多查询,但没有任何效果。我的数据库中有 500k 行,通过此查询我只能返回 5k,这是不对的。我肯定它至少是 200K。我也尝试使用 Excel 但它处理起来太多了。 有任何想法吗? 在此先感谢大家。

SELECT c.code, c.name as SCT_Name, t.name as SYNONYM_Name, count(c.code)
FROM database.Terms as t
  join database.dbo.Concepts as c on c.ConceptId = t.ConceptId
  where t.TermTypeCode = 'SYNONYM' and t.ConceptTypeCode = 'NAME_Code' and c.retired = '0'
   Group by c.code, c.name, t.name
   HAVING COUNT(c.code) > = 1

Order by c.code

如果您只想要 c.code 的副本,您的 Group By 是错误的(您的 Having 子句也是如此)。试试这个:

SELECT c.code
FROM database.Terms as t
  join database.dbo.Concepts as c on c.ConceptId = t.ConceptId
  where t.TermTypeCode = 'SYNONYM' and t.ConceptTypeCode = 'NAME_Code' and c.retired = '0'
   Group by c.code
   HAVING COUNT(c.code) > 1

这将 return 具有多个 c.code 值的所有行。

您需要使用 INTERSECT 而不是 JOIN。基本上,您在第一个 table 上执行 select,然后与第二个 table 相交。结果是重复的行。

只有 select id 列,否则相交将无法按预期工作。

with data as (
    select c.code, c.name as SCT_Name, t.name as SYNONYM_Name
    from database.Terms as t inner join database.dbo.Concepts as c
        on c.ConceptId = t.ConceptId
    where
            t.TermTypeCode = 'SYNONYM'
        and t.ConceptTypeCode = 'NAME_Code'
        and c.retired = '0'
)
select *
    --, (select count(*) from data as d2 where d2.code = data.code) as code_count
    --, count(*) over (partition by code) as code_count
from data
where code in (select code from data group by code having count(*) > 1)
order by code