Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add sequence number column into result data? [duplicate]

Possible Duplicate:
Add row number to this T-SQL query

Im using sql server 2008.

When I type: select * from Employee. The result like this:

EmpID    |    EmpName    |    Salary
-------------------------------------
DB1608   |    David      |    100000
JT2607   |    John       |    150000
AM1707   |    Ann        |    140000
ML1211   |    Mary       |    125000

But I want the result like this:

No      |    EmpID    |    EmpName    |    Salary
--------------------------------------------------
1       |    DB1608   |    David      |    100000
2       |    JT2607   |    John       |    150000
3       |    AM1707   |    Ann        |    140000
4       |    ML1211   |    Mary       |    125000

The column "No" is the auto increment number, and NOT the identity field in this table.

How can I do this?

like image 393
furyfish Avatar asked Dec 21 '22 12:12

furyfish


1 Answers

Using ROW_NUMBER() (documentation)

SELECT ROW_NUMBER() OVER (ORDER BY EmpID ASC) AS No, 
    EmpID, EmpName, Salary
FROM Employee

See in action

like image 142
Kermit Avatar answered Mar 04 '23 09:03

Kermit