1USE AdventureWorks2012;
2GO
3SELECT FirstName, LastName, TerritoryName, ROUND(SalesYTD,2,1) AS SalesYTD,
4ROW_NUMBER() OVER(PARTITION BY TerritoryName ORDER BY SalesYTD DESC)
5 AS Row
6FROM Sales.vSalesPerson
7WHERE TerritoryName IS NOT NULL AND SalesYTD <> 0
8ORDER BY TerritoryName;
9
1SELECT t.A, t.B, t.C, ROW_NUMBER() OVER (ORDER BY t.A) as newId
2 FROM dbo.tableZ AS t
3 ORDER BY t.A;
1SELECT
2 ROW_NUMBER() OVER (
3 ORDER BY first_name
4 ) row_num,
5 first_name,
6 last_name,
7 city
8FROM
9 sales.customers;
10
1Returns results where the row number meets the passed condition.
2Example: Returns the top 10 countries from the countries table.
3SELECT * FROM countries
4WHERE ROWNUM <= 10;
1SELECT ROWNUM, a.*
2FROM (SELECT customers.*
3 FROM customers
4 WHERE customer_id > 4500
5 ORDER BY last_name) a;