I have to do the following and can't figure out how to do it all correctly:
I have a table test
| Test1 | Test2 |
|---|---|
| 1 | ABC |
| 2 | DEF |
I want to duplicate this, and have the test1 column have subsequent numbering. When I do a simple insert I can of course just duplicate it all and I have what I need. But I can't get the numbering of Test1 right.
The result I'm looking for is this:
| Test1 | Test2 |
|---|---|
| 1 | ABC |
| 2 | DEF |
| 3 | ABC |
| 4 | DEF |
What I'm getting at the moment is:
| Test1 | Test2 |
|---|---|
| 1 | ABC |
| 2 | DEF |
| 1 | ABC |
| 2 | DEF |
I tried the following but did not get subsequent numbering in the test1 column:
INSERT INTO test (test1,
test2)
SELECT test,
test
FROM test; drop table if exists temp.tmp;
create temporary table tmp as
select test1, row_number() over (order by test1) rn
from test; update test
set test1 = (
select rn from temp.tmp
where temp.tmp.test1 = test.test1
);
drop table temp.tmp;
You can use a window function for this task. Specifically, you can duplicate all your rows, yet increasing the Test1 values, by adding the current total count of records to every record of your table.
Output:
Check the demo here.