Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Duplicate/Replicate data in table

Posted on 2014-02-04
Medium Priority
549 Views
I have a table of 10,000 rows that I need to multiply into 200,000. I basically want to take the data I have in the table duplicate it until there are 200,000 rows, but the empl id needs to be unique so I plan on using a sequence to create it.  Basic table structure below

Case

Emplid    case      case_desc
1             open     this case is open
2             closed   this case is closed
3             open     this case is open

Should I justinsert into case a (select  seq.nxtval, case, case_desc  from case b); until I reach 200K or is this a more efficient way.
0
Question by:klpayton
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3

LVL 74

Expert Comment

ID: 39833151
so, simply multiply the data by 20?
Try this...

change 11000  to be any number at least as big as the current max id

INSERT INTO yourtable(emplid,case,case_desc)
SELECT 11000 + ROWNUM, CASE, case_desc
FROM yourtable,
(    SELECT LEVEL
FROM DUAL
CONNECT BY LEVEL <= 20)
where rownum <= 200000
0

LVL 32

Expert Comment

ID: 39833170
Is emplid currently a unique key?
0

Author Comment

ID: 39833177
@awking00 Yes, emplid is a unique key
0

LVL 74

Expert Comment

ID: 39833185
the insert above will create 200000 unique new records,  So, if you have 10000 to start with you'll have 210000 when done.

If that's too many, Change the connect by to be   <= 19

or simply delete the extras when you're done
if you don't have 10000 rows to start, then bump the connect by up to 21 or higher as needed.

I would expect the insert above should take a few seconds at most.  Try it, if it doesn't work, simply do a rollback, if it does, then commit and you're done.
0

LVL 74

Accepted Solution

sdstuber earned 2000 total points
ID: 39833225
if you'd prefer, the insert can be entirely self-contained by doing a lookup on the max id and counting the current number of rows and adjusting the output to make sure it's exactly 200000 rows when done

This does assume you have between 1 and 199,999 rows to start with though

INSERT INTO yourtable
WITH x AS (SELECT MAX(emplid) maxid, COUNT(*) cnt FROM yourtable)
SELECT maxid + ROWNUM, CASE, case_desc
FROM yourtable,
x,
(    SELECT LEVEL
FROM DUAL
CONNECT BY LEVEL <= FLOOR(200000 / (SELECT cnt FROM x)))
WHERE ROWNUM <= 200000 - cnt;
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
###### Suggested Courses
Course of the Month7 days, 4 hours left to enroll