September 8, 2010 at 7:57 am
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
111
112
121
211
221
222
223
224
Any suggestions or inputs would help.
Thanks
September 8, 2010 at 8:03 am
what is the logic?
----------
Ashish
September 8, 2010 at 8:06 am
Something like
SELECT ROW_NUMBER() OVER(PARTITION BY EMPID, ROLE ORDER BY ROLE)
September 8, 2010 at 8:13 am
Is the table in your post the starting point of your query, or is the result.
If it is the result, I guess you want to give some sort of sequence number to the rows, based on the combination of EMPID and ROLE.
You can try to use the ranking functions for this, in specific the ROW_NUMBER function.
http://msdn.microsoft.com/en-us/library/ms186734.aspx
A select statement would be something like this:
SELECT
[EMPID]
,[ROLE]
,[SEQ]= ROW_NUMBER() OVER(PARTITION BY [EMPID],[ROLE] ORDER BY [EMPID],[ROLE])
FROM MyTable
However, to use it directly in an UPDATE statement will be difficult. For example, the first two rows have the same EMPID and ROLE, but there's no way to seperate these two rows from each other as long as SEQ does not exists.
Also, read the following article on how to ask questions in the forums, as your question was a bit confusing.
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
September 8, 2010 at 8:14 am
Great, I was taking so long to formulate my answer, that two people were faster than me 🙂
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply