Queries to Get Row Number

Here tricks queries to get row number
There is a table "MsUser" contains fields ID & Name :
0860101 Budi
0891010 Iwan
0891200 Bayu
0902310 Donny

The query could be like this below

Select a.ID,a.Name,(Select count(*) From MsUser Where ID < a.ID) From MsUser a

The results are :

0860101 Budi 0
0891010 Iwan 1
0891200 Bayu 2
0902310 Donny 3

Those are incase we are using SQL Server 2000

It would be not necessary when we are using SQL Server 2005. The query to show record number has already provided. Let's see query below :

Select ID, Name, row_number() over (order by ID asc) from MsUser

The results are :

0860101 Budi 1
0891010 Iwan 2
0891200 Bayu 3
0902310 Donny 4
May it Helps

Thanks

No comments:

Post a Comment