How To Generate Serial Numbers +add 1 In Select Statement
I know we can generate row_number in select statement. But row_number starts from 1, I need to generate from 2 and onwards. example party_code ---------- R06048 R06600 R06791 (3 r
Solution 1:
SELECT party_code, 1 + ROW_NUMBER() OVER (ORDERBY party_code) AS [serial number]
FROM myTable
ORDERBY party_code
to add: ROW_NUMBER() has an unusual syntax, and can be confusing with the various OVER and PARTITION BY clauses, but when all is said and done it is still just a function with a numeric return value, and that return value can be manipulated in the same way as any other number.
Solution 2:
I don't know much about SQL Server but either one of these will work:
SELECT party_code, 1 + ROW_NUMBER() OVER (ORDERBY party_code) AS [serial number]
FROM myTable
ORDERBY party_code
OR
SELECT party_code, serial_numer + 1AS [serial number] FROM
(SELECT party_code, ROW_NUMBER() OVER (ORDERBY party_code) AS [serial number]
FROM myTable)
ORDERBY party_code
Post a Comment for "How To Generate Serial Numbers +add 1 In Select Statement"