【问题标题】:Need to fetch only duplicate values and ignore the unique values in SQL server只需要获取重复值并忽略 SQL server 中的唯一值
【发布时间】:2021-03-27 23:32:28
【问题描述】:

表名是 SAMPLE,列是 DID、RID 和 CAT

在表中,我们看到 DID 列 100 具有相同的 CAT 值,例如 CA 重复了两次,并且 DID 200 和 500 在 CAT 列中也有重复值。所以我想在下面的输出中

100 CA
200 CA
500 OA

300 和 400 DID 没有任何重复值都是唯一的,所以在我的查询中应该排除这些记录

请在查询部分提供帮助。

表格信息:

【问题讨论】:

    标签: sql sql-server duplicates aggregate-functions


    【解决方案1】:

    您可以使用group byhaving,如下所示:

    select did, cat
      from your_table
    group by did, cat
    having count(1) > 1
    

    您也可以使用count窗口函数如下:

    select did, cat from
    (select t.*, count(1) over (partition by did, cat) as cnt
    from your_table t) t
    where cnt > 1
    

    【讨论】:

      猜你喜欢
      • 2018-01-11
      • 1970-01-01
      • 2012-10-29
      • 2011-08-10
      • 2010-11-24
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      相关资源
      最近更新 更多