ispostback
ispostback

Reputation: 412

How to find the total count of duplicate name

I have a table

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

Now I have written a Stored Procedure:

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

Now I want to return the Count of how many times a name has been replicated

Help required

Thanks in advance

Upvotes: 1

Views: 48

Answers (2)

John Cappelletti
John Cappelletti

Reputation: 82010

Small twist to Ullas query... The HAVING will return only dupes

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

Upvotes: 3

Ullas
Ullas

Reputation: 11556

Use COUNT with GROUP BY.

Query

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

Upvotes: 2

Related Questions