Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Update Seq

Author  Topic 

amsqlguy
Yak Posting Veteran

89 Posts

Posted - 2010-09-08 : 10:04:09
Guys,

I have following table in which I am trying to figure out a way to update the seq column as per the logic below by grouping empid and role.

EMPID ROLE SEQ
1 1 1
1 1 2
1 2 1
2 1 1
2 2 1
2 2 2
2 2 3
2 2 4

Any suggestions or inputs would help.

Thanks

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2010-09-08 : 11:10:16
row_number over() to the rescue!

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

kunal.mehta
Yak Posting Veteran

83 Posts

Posted - 2010-09-09 : 09:28:11
declare @Customer table
(
CustomerID int,
RoleID int,
seq int
)

insert into @Customer values (1,1,0)
insert into @Customer values (1,1,0)
insert into @Customer values (1,2,0)
insert into @Customer values (2,1,0)
insert into @Customer values (2,2,0)
insert into @Customer values (2,2,0)
insert into @Customer values (2,2,0)
insert into @Customer values (2,2,0)

select customerid,RoleID,row_number() over(partition by customerid,roleid order by customerid,roleid asc) from @Customer
Go to Top of Page
   

- Advertisement -