oracle - Transpose and insert multiple rows with differing values -


I have another complex version of the following table:

  id | First of all. Last | EMAIL 1 | John | Do | | Jdoe@example.com 1 | Mac | Johnson | Mjohnson@example.com 1 | Steven | Michaels | Smichaels@example.com 2 | Sara | Sampson | Ssampson@example.com 2 | Tom | Smith | Tsmith@example.com 2 | Jane | Rogers | Jrogers@example.com 3 | Bob | Johns | Bjohns@example.com 3 | Kim | Lane | Klane@example.com 3 | Ron | Swanson | Rswanson@example.com   

and I would like to write a query that inserts this data to appear in another table like this (table already exists): < Pre> id | First 1 Last 1 Email 1 | First 2 Last 2 Email 2 | First 3 Last 3 Email 3 1 John | Do | | Jdoe@example.com | Mac | Johnson | Mjohnson@example.com | Steven | Michaels | Smichaels@example.com 2 | Sara | Sampson | Ssampson@example.com | Tom | Smith | Tsmith@example.com | Jane | Rogers | Jrogers@example.com 3 | Bob | Johns | Bjohns@example.com | Kim | Lane | Klane@example.com | Ron | Swanson | Rswanson@example.com

I think it should be easy, but the concept is getting rid of me, what is the best practice to accomplish this? I should also mention that I have written a function, that I get an email, an index number, and a column name (i.e. getpersoninfo (2, '1', ' First ').

  Select a_id, FIRST1, LAST1, EMAIL1, FIRST2, LAST2, EMAIL2, FIRST3, LAST3, EMAIL3 (choose AIID, call) () as new_col, value Unblock (variant (v) by varchar2 (10)) RN (dump_recs_2015) on (Aiid, first_name, last_name, email, cast (line_number)) (for a call) (FIRST_name) 'FIRST1' FIRST1, 'LAST1' LAST1, 'EMAIL1' EMAIL1, 'FIRST2' FIRST2, 'LAST2' last 2, 'EMAIL2' EMAIL2, 'FIRST3' for new_ col (maximum (value)) FIRST3 , 'LAST3' LAST3, 'EMAIL3' EMAIL3));    

Since you use Oracle 11 If you are using G, you can implement both of them.

UNPIVOT Your columns are first , last and email and convert values ​​to rows. You can then enter PIVOT new name first 1 in the column, and so on: first1, last1, Varchar2 (10) ) To email 2, first 3, last 3, e-mail 3 (select id, colas-rn as new_col, value (selection id, first, last, email, artist (row_number)) for its opening) univivot (First, last, email)) (first ('FIRST1' first1, 'LAST1' Last1, 'EMAIL1' Email1, 'FIRST2' First 2, 'Last 2' Last 2, 'EMAIL2' Email 2, 'First 3' first 3, 'Last 3' T 3, email 3 'EMAIL3'))

View. It can be used again in the INSERT statement to load the data in a new table.

Edit, depending on your column name changes, you will use it:

  AIID, First 1, Last 1, Email 1, First 2, Last Choose from 2, Email 2, First 3, Last 3, Email 3 (select AIID, cola) as new_col as RNC, select from value (AIID, first_name, lastname, email, cast) (Your_t ) As varchar2 (10)) (row_number () by a_id (division by a_id by division by a_id) unpivot (value for call (first_name, last_name, email))) First ('FIRST_NAME1' is the first '1', 'LAST_NAME1', last 1, 'EMAIL1' email 1, 'FIRST_NAME2' first 2, 'LAST_NAME2' last 2, 'EMAIL2' email 2, 'FIRST_NAME3' first 3, 'LAST_NAME3' last 3, 'EMAIL3' email 3))   

View

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -