I have data that looks roughly like this:
user_id range start end
10 500 1 500
11 175
12 200
and need to transform it into something like this:
user_id range start end
10 500 1 500
11 175 501 675
12 200 676 875
so each subsequent row's start number is the previous row's end number. the current row's end number is the start number + range-1.
I was trying to do this purely in SQL but seem a bit stuck as this produces an error:
UPDATE users U
SET start = ( SELECT end+1 FROM users U2 WHERE U2.id = U.id - 1 )
WHERE U.id > 1;
which results in You can't specify target table 'users' for update in FROM clause
-- and while I'm still playing around for a solution, seem unable to find one.