SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 auto increment - group by
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cenzor
Starting Member

1 Posts

Posted - 06/17/2012 :  14:05:20  Show Profile  Reply with Quote
Hello All,

I would like to ask how to autoincrement integer value in a column based on the value in other column. I.e. if the value changes the incrementation should start again.

E.g. I have a table with column A and B. Column B is blank
And A has the following values:

A B
x
x
x
y
y
z
z
z
z

And I would like to update the column in the following way:

A B
x 10
x 20
x 30
y 10
y 20
z 10
z 20
z 30
z 40

Thank you!

Jan


tkizer
Almighty SQL Goddess

USA
35007 Posts

Posted - 06/17/2012 :  14:38:43  Show Profile  Visit tkizer's Homepage  Reply with Quote
Check out the ROW_NUMBER() function.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 06/17/2012 :  16:49:37  Show Profile  Reply with Quote

UPDATE t
B = Seq * 10
FROM 
(
SELECT ROW_NUMBER() OVER (PARTITION BY A ORDER BY (SELECT 1)) AS Seq
FROM  Table
)t


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000