Select Row Number in a SQL Statement

Select Row Number in a SQL Statement

Spesso mi è capitato di dover selezionare, come risultato di una query SQL, un numero incrementale di riga; una sorta di identity fittizio, un contatore di riga (row number).

Ecco lo snippet:

USE [Northwind] GO SELECT RANK() OVER (ORDER BY a.LastName, A.FirstName) AS [Row Number], A.LastName AS [Surname], A.FirstName AS [Name]FROM Employees A ORDER BY [Row Number]

Il risultato, sul Database Northwind, è il seguente:

[Raw Number] | [Surname]      | [Name]
1                         | Cognome 1   | Nome 1
2                         | Cognome 2   | Nome 2
3                         | Cognome 3   | Nome 3
….

Testato su MS SQL Server 2005/2008.

1 commento

comments user
Anonimo

It’s really a great and useful piece of info. I’m glad that you simply shared this helpful
info with us. Please keep us up to date like this.
Thank you for sharing.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *