Kiera Smith
Kiera Smith

Reputation: 331

SQL Logic In Query

I am trying to implement some logic in an sql query to create a table similar to:

Count   Name
1         a
1         a
1         a
2         b
2         b
3         c
4         d
4         d
5         e
5         e
5         e
5         e

As you can see, the logic is a new count starts with each name change, but this count remains as if if the name is not changed.

Upvotes: 0

Views: 58

Answers (1)

Lamak
Lamak

Reputation: 70638

You need DENSE_RANK():

SELECT Name,
       DENSE_RANK() OVER(ORDER BY Name) [Count]
FROM dbo.YourTable;

Upvotes: 1

Related Questions