Solved

Duplicate/Replicate data in table

Posted on 2014-02-04
5
541 Views
Last Modified: 2014-02-05
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
Comment
Question by:klpayton
  • 3
5 Comments
 
LVL 73

Expert Comment

by:sdstuber
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 31

Expert Comment

by:awking00
ID: 39833170
Is emplid currently a unique key?
0
 

Author Comment

by:klpayton
ID: 39833177
@awking00 Yes, emplid is a unique key
0
 
LVL 73

Expert Comment

by:sdstuber
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 73

Accepted Solution

by:
sdstuber earned 500 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now