0

I want to use a static-sized table like this:

+------+----------+-----------+
|  id  | col1     | col2      |
+------+----------+-----------+
|  1   | a        | x         |
+------+----------+-----------+
|  2   | b        | y         |
+------+----------+-----------+
|  3   | c        | z         |
+------+----------+-----------+

Is there a way to shift the column data upwards when I update [3, col1] for example? Table should look like this...

+------+----------+-----------+
|  id  | col1     | col2      |
+------+----------+-----------+
|  1   | b        | x         |
+------+----------+-----------+
|  2   | c        | y         |
+------+----------+-----------+
|  3   | d*       | z         |
+------+----------+-----------+

*New value in [row3, col1] and column data has been shifted up; thanks in advance.

1 Answer 1

1

You can do this with an update/join:

update table t left join
       table tnext
       on t.id = tnext.id - 1
    set t.col1 = (case when tnext.id is null then 'd' else tnext.col1 end);
Sign up to request clarification or add additional context in comments.

2 Comments

I hope you don't think of this as an obtuse request, but would you mind explaining the statement, so I can understand this join stuff, it befuddles my brain; if not, no worries.
This is not the forum for explaining how joins work. Try google'ing or reading database documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.