如何查找重复名称的总数

时间:2016-07-24 17:39:18

标签: sql-server stored-procedures sql-server-2008-r2

我有一张桌子

CREATE TABLE [dbo].[tblTeams](
[Id] [int] IDENTITY(1,1) NOT NULL,
[TeamId] [varchar](5) NOT NULL,
[TeamName] [varchar](100) NOT NULL,
[PlayerName] [varchar](100) NOT NULL,
[PlayerNickName] [varchar](100) NULL,
[Status] [int] NULL

现在我写了一个存储过程:

Alter Proc [dbo].[SpGetDuplicateName]
    @PlayerName varchar(200)
AS
Begin
(
    Select PlayerName,TeamName,TeamId 
    from tblTeams where PlayerName like @PlayerName 
)
END

现在我想返回一个名称被复制多少次的计数

需要帮助

提前致谢

2 个答案:

答案 0 :(得分:3)

对Ullas查询的小扭曲...... HAVING将仅返回欺骗

SELECT PlayerName, COUNT(PlayerName) AS [Count]
FROM tblTeams
WHERE PlayerName LIKE '%word_to_search%'
GROUP BY PlayerName;
HAVING COUNT(*)>1

答案 1 :(得分:2)

COUNTGROUP BY一起使用。

<强>查询

SELECT PlayerName, COUNT(PlayerName) AS [Count]
FROM tblTeams
WHERE PlayerName LIKE '%word_to_search%'
GROUP BY PlayerName;