|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I have a query that lists various records, but also should put a row
count next to each. For instance No. Name 1 John Smith 2 Ravi Patel 3 Ronnie Oshinsky etc. the problem is I don't know how to get the consecutive numbers to go with the names. No numbers are stored in the database, just names and a primary key field. The primary key field is not consecutive because of deletes that sometimes happen. So I cannot use the primary key field. Is there any way to do this? Thanks, Marvin |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On SQL Server 2005 you can use ROW_NUMBER() to generate consecutive numbers.
You have to decide what the ordering will be based on (the name column, the primary key column, etc.). Here is how it may look: SELECT ROW_NUMBER() OVER(ORDER BY name) AS 'No.', [name] FROM Foo; Note the ORDER BY clause in OVER. That will define the number sequence. HTH, Plamen Ratchev http://www.SQLStudio.com |
|
![]() |
| Outils de la discussion | |
|
|