-- Excerpted from "T-SQL programming entry classic"
The row_number () function returns an increasing integer based on the value of the order by clause passed to the function as a parameter. If the order by value of row_number matches the order in the result set, the return value is incremented and arranged in ascending order. If the value of the order by clause of row_number is different from the order in the result set, these values are not listed in order, but they represent the order of the order by clause of the row_number function. The example and result are as follows:
SelectProductcategoryid, name, row_number ()Over(Order ByName)AsRownumFromPorduction. productcategoryOrder ByName
The order by clause in the row_number () call matches the query results sequentially, so these results are listed in order.
However, when another order by clause is used in a function call, these values are unordered.
SelectProductcategoryid, name, row_number ()Over(Order ByName)AsRownumFromPorduction. productcategoryOrder ByProductcategoryid