6

I have this table. Some of the rows have duplicate values in the Kanji column.

How can I show these rows where the same Kanji appears more than once?

CREATE TABLE [dbo].[Phrase] (
    [PhraseId]              UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
    [English]               NVARCHAR (250)   NOT NULL,
    [Kanji]                 NVARCHAR (250)   NULL,
    PRIMARY KEY CLUSTERED ([PhraseId] ASC) );
Alan2
  • 23,493
  • 79
  • 256
  • 450

6 Answers6

7

You can use a GROUP BY statement by that column and specify a constraint that COUNT(*) of that group is larger than 1, so:

SELECT [kanji]
FROM [dbo].[Phrase]
GROUP BY [kanji]
HAVING COUNT(*) > 1
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
3

Group by with having will get which words are duplicates:

SELECT Kanji FROM Phrase
GROUP BY Kanji
HAVING COUNT(*)>1
Arion
  • 31,011
  • 10
  • 70
  • 88
1

select Kanji from MyTable
Group By Kanji
Having Count(*) > 1

I'd suggest having a full-text index on the column you want...

Leonardo
  • 10,737
  • 10
  • 62
  • 155
0
;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
Mazhar
  • 3,797
  • 1
  • 12
  • 29
  • 1
    Thank you for this code snippet, which may provide some immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its educational value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight May 16 '17 at 12:36
0
SELECT COUNT(*) AS `doubles` FROM [table] GROUP BY `Kanji` HAVING `doubles` > 1;
Remco K.
  • 644
  • 4
  • 19
0

It appears that he wants the duplicate rows, not just the values that are duplicated. One way to do this is to find the duplicate values in that column and then JOIN back to the original table to see the entire row. See the query below:

; 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
EnterTheCode
  • 474
  • 5
  • 20