Solved

HOW TO ASSIGN CONSECUTIVE NUMBERS IN SQL

Posted on 2012-04-12
5
597 Views
Last Modified: 2012-06-27
I have a SQL Server table with the following fields:
ID
PRODUCT
ORIGINATION DATE
It contains several different ID's, each with up to 40 PRODUCTs. I need to order the products by ORIGINATION DATE, and then assign a new field with the consecutive number. For example, source data:
ID 1
PRODUCT A
ORIGINATION DATE MARCH 2, 2012
ID 1
PRODUCT B
ORIGINATION DATE DECEMBER 10, 2011
ID 1
PRODUCTO C
ORIGINATION DATE DECEMBER 14, 2011
I need the following output:
ID 1
PRODUCT A
ORIGINATION DATE MARCH 2, 2012
CONSECUTIVE NUMBER: 3
ID 1
PRODUCT B
ORIGINATION DATE DECEMBER 10, 2011
CONSECUTIVE NUMBER: 1
ID 1
PRODUCTO C
ORIGINATION DATE DECEMBER 14, 2011
CONSECUTIVE NUMBER: 2
0
Comment
Question by:CASH59
  • 3
  • 2
5 Comments
 
LVL 39

Expert Comment

by:appari
ID: 37840660
try this
Select  ID, PRODUCT, [ORIGINATION DATE] , Row_numer() over(order by [ORIGINATION DATE]) [CONSECUTIVE NUMBER]
from tableName
0
 
LVL 39

Expert Comment

by:appari
ID: 37840661
and if you want to reset the number within each ID try this

Select  ID, PRODUCT, [ORIGINATION DATE] , Row_numer() over(partition by ID order by [ORIGINATION DATE]) [CONSECUTIVE NUMBER]
from tableName
0
 

Author Comment

by:CASH59
ID: 37840677
I need to create the CONSECUTIVE NUMBER first, it would be a new field.
thanks
0
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 37840702
then try this,

to Add new column execute the following script
ALTER TABLE tableName ADD
	[CONSECUTIVE NUMBER] int NULL

Open in new window


To update the data execute the following script

;with a as (
Select  ID, PRODUCT, [ORIGINATION DATE] , Row_numer() over(partition by ID order by [ORIGINATION DATE]) CON_NUM
from tableName)
Update a set [CONSECUTIVE NUMBER] =CON_NUM

Open in new window

0
 

Author Comment

by:CASH59
ID: 37840746
It works. Thanks
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS 2013 - Creating a summarized report 19 33
SQL Server 2012 r2 - Varible Table 3 23
Help creating a spatial object in SQL Server 4 20
TSQL query to generate xml 4 32
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question