patternModerate
Can the sorting from an ORDER BY statement be saved explicitly with only one UPDATE statement?
Viewed 0 times
sortingcantheorderupdatestatementwithexplicitlyonefrom
Problem
I have this table:
Sorting this table gives this result:
Now I want to update the table and save the order in the position column:
Can this be done with a single query or do I have to manually loop over all rows and do a manual update?
In case the order is not fully defined (e.g. for USB cable and Keyboard above) I just arbitrarily decided the order.
SELECT * FROM items
id | item | position
---|-----------|----------
1 | USB cable | 0
2 | SD card | 4
3 | Mouse | 2
4 | Keyboard | 0
5 | Monitor | 3Sorting this table gives this result:
SELECT * FROM items ORDER BY position
id | item | position
---|-----------|----------
4 | Keyboard | 0
1 | USB cable | 0
3 | Mouse | 2
5 | Monitor | 3
2 | SD card | 4Now I want to update the table and save the order in the position column:
SELECT * FROM items
id | item | position
---|-----------|----------
4 | Keyboard | 1
1 | USB cable | 2
3 | Mouse | 3
5 | Monitor | 4
2 | SD card | 5Can this be done with a single query or do I have to manually loop over all rows and do a manual update?
In case the order is not fully defined (e.g. for USB cable and Keyboard above) I just arbitrarily decided the order.
Solution
UPDATE
T1
SET
position = T2.rn
FROM
myTable T1
JOIN
(
SELECT
id,
ROW_NUMBER() OVER (ORDER BY position) AS rn
FROM
myTable
) T2 ON T1.id = T2.idNote: the order of "Keyboard" and "USB cable" is arbitrary. They both have position = 0
To tie-break positions based on item, add a secondary sort
ROW_NUMBER() OVER (ORDER BY position, item) AS rnIf you have duplicate position,item pairs this will be arbitrary too...
Code Snippets
UPDATE
T1
SET
position = T2.rn
FROM
myTable T1
JOIN
(
SELECT
id,
ROW_NUMBER() OVER (ORDER BY position) AS rn
FROM
myTable
) T2 ON T1.id = T2.idROW_NUMBER() OVER (ORDER BY position, item) AS rnContext
StackExchange Database Administrators Q#5146, answer score: 12
Revisions (0)
No revisions yet.