如何从一张表中只获取重复的数据?

发布于 2024-12-03 16:49:21 字数 48 浏览 0 评论 0原文

我有一张表和许多数据,例如重复值和单个值。 但我只想获取重复值数据,而不是单个值。

I have one table and many data like duplicate values and single values.
But I want to get only duplicate value data's , not single value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往事风中埋 2024-12-10 16:49:21
SELECT columnWithDuplicates, count(*) FROM myTable
GROUP BY columnWithDuplicates HAVING (count(*) > 1);
SELECT columnWithDuplicates, count(*) FROM myTable
GROUP BY columnWithDuplicates HAVING (count(*) > 1);
灯角 2024-12-10 16:49:21

上面的查询将显示重复的值。一旦您将其提供给业务用户,他们的下一个问题将是发生了什么?这些是怎么到那里的?重复项有模式吗?通常更能提供信息的是查看包含这些值的整行,以帮助确定为什么存在重复项。

-- this query finds all the values in T that
-- exist in the derived table D where D is the list of
-- all the values in columnWithDuplicates that occur more than once
SELECT DISTINCT
    T.* 
FROM
    myTable T 
    INNER JOIN 
    (
        -- this identifies the duplicated values
        -- courtesy of Brian Roach
        SELECT 
            columnWithDuplicates
        ,   count(*) AS rowCount
        FROM 
            myTable 
        GROUP BY 
            columnWithDuplicates 
        HAVING 
            (count(*) > 1)
    ) D 
        ON D.columnWithDuplicates = T.columnWithDuplicates

The above query will show the duplicated values. And once you provide that to a business user, their next question will be what happened? How did these get there? Is there a pattern to the duplicates? What's often more informative is to see the whole rows containing those values to help determine why there are duplicates.

-- this query finds all the values in T that
-- exist in the derived table D where D is the list of
-- all the values in columnWithDuplicates that occur more than once
SELECT DISTINCT
    T.* 
FROM
    myTable T 
    INNER JOIN 
    (
        -- this identifies the duplicated values
        -- courtesy of Brian Roach
        SELECT 
            columnWithDuplicates
        ,   count(*) AS rowCount
        FROM 
            myTable 
        GROUP BY 
            columnWithDuplicates 
        HAVING 
            (count(*) > 1)
    ) D 
        ON D.columnWithDuplicates = T.columnWithDuplicates
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文