Cari Blog Ini

25 Maret 2013

How To Add Auto Running Number in Selected Data


Apply To:
Microsoft SQL Server 2008 R2

Problem: 

Want to add a auto running number in selected data. 



Solution:

1. Example, dummy data is

------------------------------------------------------------------------
------------------------------------------------------------------------
select Voucher,DATE,Currency,Nominal_CC from VOUCHER
order by Voucher
------------------------------------------------------------------------
------------------------------------------------------------------------
Results Messages:



2. Add this query
------------------------------------------------------------------------
------------------------------------------------------------------------
select

ROW_NUMBER() over( order by Voucher )  [ID_Number]

,Voucher,DATE,Currency,Nominal_CC
from Voucher
order by Voucher
------------------------------------------------------------------------
------------------------------------------------------------------------
Results Messages:


Source:
http://ask.sqlservercentral.com/questions/97751/calculations-using-information-from-the-previous-r.html