3

This might sound a bit confusing at first. But I have a table with data and I want to create duplicates of all the rows in this table. So, if i do:

SELECT *
FROM the_table

it lists all the data in this table. Now, i want to create a copy of all returned results, except for that I want to change data for one column (the date). This will make the new rows unique.

The reason I want to do this is because I want more data in this table since im building statistics out of it for testing purposes. So, if I have this:

**Column1   Column2   Column3**
abc       aaa         bbb
abcd      aaaa        bbbb
abcde     aaaaa       bbbbb

The table will now contain:
**Column1   Column2   Column3**
abc       aaa         bbb
abcd      aaaa        bbbb
abcde     aaaaa       bbbbb
abc       aaa         bbb_new
abcd      aaaa        bbbb_new
abcde     aaaaa       bbbbb_new

5 Answers 5

6
insert into your_table
   select col1, col2, concat(col3, '_new') from your_table
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I got it to work. Altho I wanted up update the current date in the reproduced lines. I used Dateadd for this purpose
3

Consider making a Cartesian Join on your table. This will give you way more data quickly :)

Comments

2
INSERT INTO TABLEDUPLICATES
SELECT * FROM the_table


SELECT * FROM TABLEDUPLICATES UNION 
SELECT * FROM the_table 

Comments

2

Assuming there is an identity column (ID) you might generate dates (A_Date) like this:

insert into the_table (Column1, Column2, A_Date)
select Column1, Column2, A_Date + (rand(ID) - 0.5) * 100
  from the_table

Comments

2

To duplicate rows (all columns) you simply could use

insert into tblname
select * from tblname

to change one column that can be modified to

insert into tblname
select column1, column2, 'fixedvalueforcolumn3' from tblname

But you need a unique value for column 3, so you have to change 'fixedvalueforcolumn3' to a function that will generate some random (unique) value (date in your case) for column 3

insert into tblname
    select column1, column2, generateRandomValue() from tblname

Hope that will help you

Comments

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.