How to generate serial numbers + Add 1 to select statement

I know that we can generate row_number in a select statement. But row_number starts at 1, I need to generate from 2 onwards.

Example

party_code
----------
R06048
R06600
R06791
(3 row(s) affected)
I want it like

party_code serial number
---------- -------------
R06048       2
R06600       3
R06791       4 

Current I use the select statement below to generate a regular line number.

 SELECT party_code, ROW_NUMBER() OVER (ORDER BY party_code) AS [serial number]
FROM myTable
ORDER BY party_code

How can I change the select select statement and start with 2?

+5
source share
2 answers
SELECT party_code, 1 + ROW_NUMBER() OVER (ORDER BY party_code) AS [serial number]
FROM myTable
ORDER BY party_code

add: it ROW_NUMBER()has an unusual syntax and can be misleading to various sentences OVERand PARTITION BY, but when everything is said and done, it is still just a function with a numeric return value and that the return value can be manipulated just like any other number.

+16

SQL Server, :

SELECT party_code, 1 + ROW_NUMBER() OVER (ORDER BY party_code) AS [serial number]
FROM myTable
ORDER BY party_code

SELECT party_code, serial_numer + 1 AS [serial number] FROM
(SELECT party_code, ROW_NUMBER() OVER (ORDER BY party_code) AS [serial number]
FROM myTable)
ORDER BY party_code
+2

All Articles