numbering repetaed records 12 123 1 1234 etc sql ms 2005

Posted on 2008-10-16
Last Modified: 2010-03-19
I have a table with n records (1 field each) I nee an sqk query that will order them by field and add a consecutive number to each repeted record starting from 1 then 2 then 3 ..until the next different record(field) is reached abd star againnwith 1,2 123,1, etc

John 1
John 2
Mike 1
Sam  1
Sam 2
Sam 3
Question by:robrodp
  • 3
  • 2
LVL 39

Accepted Solution

BrandonGalderisi earned 500 total points
ID: 22736714
This should do:

select YourField, row_number() over (partition by YourField order by yourField) from YourTable


Author Comment

ID: 22736801
select email row_number() over (partition by email order by email) from icrea

I get this error:

Incorrect syntax near ')'.
LVL 39

Expert Comment

ID: 22736861
you forgot the comma between email and row_number()
LVL 39

Expert Comment

ID: 22736868
and the reason it is giving an error is because it thinks you are trying to alias email as row_number

Author Comment

ID: 22736910
Got it....


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now