I have the following table:
UserID Sub EID
1 TA 1
1 TA 6
1 TA 2
2 TA 7
2 GB 7
3 TA 6
3 TA 1
3 GB 7
3 MX 7
3 MX 6
I am trying to run a sql statement that will transform the “Sub” field by adding a number starting from 1 every time that there is a duplicate for a particular userid. Below is what the table should look like after I apply the statement. I don’t know how this can be accomplished. I am using SQL Server 2008.
UserID Sub EID
1 TA1 1
1 TA2 6
1 TA3 2
2 TA1 7
2 GB1 7
3 TA1 6
3 TA2 1
3 GB1 7
3 MX1 7
3 MX2 6
Thanks in advance for your help.
Use
ROW_NUMBER()OVER. This example uses a table variable:Result: