Search code examples
sql-servercountrecords

Counting Retrieved Records from SQL Server


I just want to ask if how I may be able to create a dynamic numbering column based from what I will be retrieving from the database?

Ex.

Table Reservations
|ReservationNo----ClientNo------DateAdded----DateModified|
|1             |    1        |   01-01-01  |  01-01-01 |  
|2             |    2        |   01-01-01  |  01-01-01 | 
|3             |    2        |   01-01-01  | 01-01-01  | 
|4             |    2        |   01-01-01  | 01-01-01  | 
|5             |    1        |   01-01-01  | 01-01-01  | 
|6             |    3        |   01-01-01  | 01-01-01  |              
|7             |    3        |   01-01-01  | 01-01-01  | 
|8             |    2        |   01-01-01  | 01-01-01  | 
|9             |    1        |   01-01-01  | 01-01-01  | 
|10            |    1        |   01-01-01  | 01-01-01  | 

When I execute the statement below...

SELECT * FROM Table WHERE ClientNo = '1'

Result :

**Counter**-----ReservationNo----Client--------DateAdded----DateModified|
|1       |      1          |    1        |   01-01-01  |  01-01-01 |  
|2       |      5          |    1        |   01-01-01  | 01-01-01  | 
|3       |      9          |    1        |   01-01-01  | 01-01-01  | 
|4       |      10         |    1        |   01-01-01  | 01-01-01  | 

Solution

  • Looks like you're searching for the ROW_NUMBER function, see http://msdn.microsoft.com/de-de/library/ms186734.aspx