sql - 查找列中具有重复值的行

标签 sql postgresql duplicates aggregate-functions window-functions

我有一个表author_data:

 author_id | author_name
 ----------+----------------
 9         | ernest jordan
 14        | k moribe
 15        | ernest jordan
 25        | william h nailon 
 79        | howard jason
 36        | k moribe

现在我需要的结果是:

 author_id | author_name                                                  
 ----------+----------------
 9         | ernest jordan
 15        | ernest jordan     
 14        | k moribe 
 36        | k moribe

也就是说,对于出现重复的名称,我需要 author_id。我试过这个声明:

select author_id,count(author_name)
from author_data
group by author_name
having count(author_name)>1

但它不起作用。我怎样才能得到这个?

最佳答案

我建议 window function在子查询中:

SELECT author_id, author_name  -- omit the name here if you just need ids
FROM (
   SELECT author_id, author_name
        , count(*) OVER (PARTITION BY author_name) AS ct
   FROM   author_data
   ) sub
WHERE  ct > 1;

您将认识到基本的聚合函数 count()。它可以通过附加 OVER 子句变成窗口函数 - 就像任何其他聚合函数一样。

通过这种方式,它可以每个分区 来计算行数。瞧。

它必须在子查询中完成,因为不能在同一 SELECTWHERE 子句中引用结果(发生在之后 哪里)。见:

在没有窗口函数的旧版本(v.8.3 或更早版本)中 - 或者通常 - 这个替代方案执行得非常快:

SELECT author_id, author_name  -- omit name, if you just need ids
FROM   author_data a
WHERE  EXISTS (
   SELECT FROM author_data a2
   WHERE  a2.author_name = a.author_name
   AND    a2.author_id <> a.author_id
   );

如果您关心性能,请在 author_name 上添加索引。

关于sql - 查找列中具有重复值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22722870/

相关文章:

python - 通过 id 删除 Python 列表中的重复项

.net - 如何删除 XML 文件中的重复值并保留最后一个值?

mysql - 多列索引需要添加主键吗?

mysql - 如何让 Round() 的第二个参数与列一起使用?

sql - 优化 SQL 查询

python - psycopg2 中带有标识符的动态 SQL?

Mysql 如何插入多个表并进行重复键检查?

sql - 使用 GROUP BY 和 HAVING 重构 MySQL

mysql - 从另一个网络表获取数据

postgresql - 我如何在 postgresql 中查询枚举的值