查询仅获取重复数据

时间:2009-07-17 09:00:27

标签: sql sql-server-2005

我有一张数据表

                   ID     Name

                    1      John
                    2      Robert
                    3      John
                    4      Sam
                    5      Jack
                    6      Sam

      Now i want ony the the duplicate names ony through query

         ie..,

                  Name

                  John
                  Sam

4 个答案:

答案 0 :(得分:12)

SELECT Name
FROM YourTable
GROUP BY Name
HAVING COUNT(*) > 1

答案 1 :(得分:5)

CREATE TABLE MyTable (
    ID int
    , Name nvarchar(50)
)

INSERT MyTable VALUES ( 1, 'John' )
INSERT MyTable VALUES ( 2, 'Robert' )
INSERT MyTable VALUES ( 3, 'John' )
INSERT MyTable VALUES ( 4, 'Sam' )
INSERT MyTable VALUES ( 5, 'Jack' )
INSERT MyTable VALUES ( 6, 'Sam' )

SELECT
    Name
FROM
    MyTable
GROUP BY
    Name
HAVING
    COUNT(*) > 1

DROP TABLE MyTable

结果:

Name
--------------------------------------------------
John
Sam

答案 2 :(得分:0)

with temp as  (
    select Name, count(Name) as countOfNames
    from myTable
    group by Name
    )
select Name from temp 
where countOfNames > 1

答案 3 :(得分:0)

Code = YourSearchedItem

Set TargetCell = Range("A:A").Find(What:=Code)

CurrRow = TargetCell.Row    ' Will Return Row number