如何在具有相同两列的表中列出重复行?

时间:2017-05-16 14:00:46

标签: sql-server

我有这张桌子:

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

我想要的是获取用于检测重复的所有重复行(包括英语和汉字)列的列表。

换句话说,如果有这样的行:

English   Kanji

aa        bb
aa        cc
aa        bb
aa        dd
aa        ee
aa        dd

我想要一份报告显示

aa        bb
aa        dd

2 个答案:

答案 0 :(得分:2)

试试这个:

select English,Kanji from Phrase
group by English,Kanji
having count(*)>1

答案 1 :(得分:0)

使用Row_Number()我们可以做到

;WITH CTE(English,Kanji)
as
(
select 'aa','bb' UNION ALL
select 'aa','cc' UNION ALL
select 'aa','bb' UNION ALL
select 'aa','dd' UNION ALL
select 'aa','ee' UNION ALL
select 'aa','dd' 
)
SELECT English
    ,Kanji
FROM (
    SELECT *
        ,ROW_NUMBER() OVER (
            PARTITION BY English
            ,Kanji ORDER BY English
                ,Kanji
            ) AS Seq
    FROM CTE
    ) dt
WHERE dt.Seq <> 1

输出

English Kanji
-------------
aa      bb
aa      dd