Ads 468x60px

Wednesday, September 3, 2014

SQL Server : How to generate a sequential number

In order to generate a sequential number in SQL Server, you can use the keyword 'ROW_NUMBER' in its correct format as follows:
  1. type in the keyword 'NOW_NUMBER'
  2. follow it by the keyword 'OVER', it takes two parameter:
    1. The first one is to  indicate the column in which you want to reset the sequential number based on
    2. The second is the order of that data in which the sequential number come along with.
    3. Note: The fist parameter is optionally whereas the second is manadatory.
  3. give an alias for the column using the keyword 'AS'

As an example how to generate a sequential number in SQL Server, look at the following example:

SELECT ROW_NUMBER() OVER(PARTITION BY ColumnName ORDER BY AnotherColumnName) AS ColumnAlias


References:

No comments:

Post a Comment