关于sql server:如何从表中列出重复值?

How can I list duplicate values out of a table?

本问题已经有最佳答案,请猛点这里访问。

我有这张桌子。 某些行在Kanji列中具有重复值。

如何在同一个汉字不止一次出现的地方显示这些行?

1
2
3
4
5
CREATE TABLE [dbo].[Phrase] (
    [PhraseId]              UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
    [English]               NVARCHAR (250)   NOT NULL,
    [Kanji]                 NVARCHAR (250)   NULL,
    PRIMARY KEY CLUSTERED ([PhraseId] ASC) );

您可以使用该列的GROUP BY语句并指定该组的COUNT(*)大于1的约束,因此:

1
2
3
4
SELECT [kanji]
FROM [dbo].[Phrase]
GROUP BY [kanji]
HAVING COUNT(*) > 1

分组将获得哪些单词是重复的:

1
2
3
SELECT Kanji FROM Phrase
GROUP BY Kanji
HAVING COUNT(*)>1


从MyTable中选择汉字
由汉字组成
计数(*)> 1

我建议在你想要的列上有一个全文索引...


看起来他想要重复的行,而不仅仅是重复的值。 一种方法是在该列中找到重复值,然后JOIN回原始表以查看整行。 请参阅以下查询:

1
2
3
4
5
6
7
8
9
10
11
12
13
; WITH DuplicateKanji AS -- Query for duplicate values
(
    SELECT
        Kanji
    FROM Phrase
    GROUP BY Kanji
    HAVING COUNT(*)>1
)
SELECT -- Query to retrieve rows that were duplicates from above query
    p.*
FROM DuplicateKanji dk
    INNER JOIN Phrase p
    ON dk.Kanji = p.Kanji

1
SELECT COUNT(*) AS `doubles` FROM [table] GROUP BY `Kanji` HAVING `doubles` > 1;

1
2
3
4
5
6
7
8
;with cteDuplicates
AS(
    SELECT *
        ,ROW_NUMBER()OVER (PARTITION BY Kanji ORDER BY Kanji) 'Dup'
    FROM dbo.Phrase
)
SELECT * FROM cteDuplicates D
WHERE D.Dup > 1