Solved

Duplicate/Replicate data in table

Posted on 2014-02-04
5
544 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 74

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 32

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 74

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 74

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

820 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