I’m using SQL Server 2008.
I have two Tables: User_master and Item_master.
There is a User with user_id = 10.
|---------|
| user_id |
|---------|
| 10 |
|---------|
There are 5 Items with item_id = 20 to 24.
|---------|---------|------------|
| item_id | user_id | item_order |
|---------|---------|------------|
| 20 | 10 | 0 |
|---------|---------|------------|
| 21 | 10 | 0 |
|---------|---------|------------|
| 22 | 10 | 0 |
|---------|---------|------------|
| 23 | 10 | 0 |
|---------|---------|------------|
| 24 | 10 | 0 |
|---------|---------|------------|
There is one more column in Item_master that is item_order(int).
I want to place item_order = 0 to 4 in all these rows with only single query.
Is it possible?
EDIT :
item_id is not supposed to be in order.
For example, instead of 20,21,22,23,24; it could be 20,25,31,47,58.
You can use the
row_number()window function to assign an increasing number to each row with the sameuser_id. A subquery is required because you cannot use window functions directly in thesetclause.Live example at SQL Fiddle.