Solved

HOW TO ASSIGN CONSECUTIVE NUMBERS IN SQL

Posted on 2012-04-12
5
613 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

732 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