The row_number function is an extremely powerful tool that I use to simplify and optimize solutions to many problems.

Occasionally, I needed to calculate nondeterministic row numbers where order didn’t matter.

For example, suppose you have the following t1 table:

  1. SET nocount ON;
  2. USE tempdb;
  3. GO
  4. IF object_id('dbo.t1') IS NOT NULL
  5. DROP TABLE dbo.t1;
  6. GO
  7. CREATE TABLE dbo.t1(col1 VARCHAR(10) NOT NULL);
  8. INSERT INTO dbo.t1(col1) VALUES('c');
  9. INSERT INTO dbo.t1(col1) VALUES('a');
  10. INSERT INTO dbo.t1(col1) VALUES('b');
  11. INSERT INTO dbo.t1(col1) VALUES('a');
  12. INSERT INTO dbo.t1(col1) VALUES('b');
  13. INSERT INTO dbo.t1(col1) VALUES('b');
  14. INSERT INTO dbo.t1(col1) VALUES('c');
  15. INSERT INTO dbo.t1(col1) VALUES('a');
  16. INSERT INTO dbo.t1(col1) VALUES('b');
  17. INSERT INTO dbo.t1(col1) VALUES('a');
  18. GO

You need to return all rows from t1 along with unique incrementing row numbers in no particular order. If order doesn’t matter, of course you can always specify an existing column from the table in the row_number function’s order by clause:

  1. SELECT col1, ROW_NUMBER() OVER(ORDER BY col1) AS rownum
  2. FROM dbo.t1;

However, the execution plan for the query would involve sorting (or an index order scan if an index exists on the sort column). Here’s the plan you get for the above query:

  1. |--Sequence Project(DEFINE:(\[Expr1004\]=row_number))
  2. |--Compute Scalar(DEFINE:(\[Expr1006\]=(1)))
  3. |--Segment
  4. |--Sort(ORDER BY:(\[tempdb\].\[dbo\].\[t1\].\[col1\] ASC))
  5. |--Table Scan(OBJECT:(\[tempdb\].\[dbo\].\[t1\]))

If the order of the row numbers doesn’t matter to you, you’d probably rather not pay the sort penalty. The problem is that SQL Server doesn’t allow a constant in the row_number function’s order by clause. Try running the following query:

  1. SELECT col1, ROW_NUMBER() OVER(ORDER BY 0) AS rownum
  2. FROM dbo.t1;

And you will get the following error:

  1. Msg 5309, Level 16, State 1, Line 1
  2. Windowed functions do NOT support constants AS ORDER BY clause expressions.

Until recently, in order to avoid sorting I defined a table expression (CTE or derived table) based on a query that returns the table rows along with a constant (call it const). In the outer query I invoked the row_number function with order by const:

  1. with c as
  2. (
  3. select col1, 0 as const from dbo.t1
  4. )
  5. select col1, row_number() over(order by const) as rownum
  6. from c;

Here, the optimizer is smart enough to realize that sorting is not required. Here’s the plan I got for this query (notice there’s no sort operation):

  1. |--Sequence Project(DEFINE:(\[Expr1005\]=row_number))
  2. |--Compute Scalar(DEFINE:(\[Expr1007\]=(1)))
  3. |--Segment
  4. |--Table Scan(OBJECT:(\[tempdb\].\[dbo\].\[t1\]))

Even though this technique avoids sorting, it is a bit awkward. Recently I got a tip from a T. Wong how to achieve the same thing without the need for a table expression—simply specify order by (select 0)!

Here’s the solution query with the new technique:

  2. FROM dbo.t1;

It is much more elegant than the previous technique, and also here the plan shows that the optimizer realized that sorting is not needed:

  1. |--Sequence Project(DEFINE:(\[Expr1006\]=row_number))
  2. |--Compute Scalar(DEFINE:(\[Expr1008\]=(1)))
  3. |--Segment
  4. |--Compute Scalar(DEFINE:(\[Expr1005\]=(0)))
  5. |--Table Scan(OBJECT:(\[tempdb\].\[dbo\].\[t1\]))