2011-09-29 69 views
5

我知道我們可以在select語句中生成row_number。但row_number從1開始,我需要從2開始生成。如何生成序列號+在select語句中添加1

例如

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

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

我使用下面的select語句生成常規行號電流。

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

如何修改上面的select語句並從2開始?

+8

哦,你自己要去踢...... – AakashM

+1

你有沒有嘗試'ROW_NUMBER()+ 1'? –

回答

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

補充:ROW_NUMBER()有一個不尋常的語法,並且可能會造成混淆與各種OVERPARTITION BY條款,但是當一切都說過和做過它仍然只是一個數字返回值的功能,以及回報價值可以像任何其他數字一樣操縱。

+1

+1你的低垂果實,先生。好的解釋,但。 :) –

2

我不知道很多關於SQL Server,但這些要麼一會工夫:

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

OR

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 
相關問題