Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)

I acknowledge that we can generate row_number in the select statement. Though row_number starts from 1, I have to generate from 2 and 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 

Currently, I am using the below select statement for generating regular row 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 above select statement and start from 2?

1 Answer

0 votes
by (12.7k points)

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

The ROW_NUMBER() contains an unusual syntax and can be confusing with the numerous OVER and PARTITION BY clauses, however when all is said and done it is still simply a function with a numeric return value, and that return value can be manipulated in a similar way as any other number.

Want to become an expert in SQL? Join the SQL course fast!

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
2 answers
0 votes
2 answers

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...