CREATE New Table from existing tables [message #600105] |
Thu, 31 October 2013 17:20 |
|
arifugurel
Messages: 1 Registered: October 2013
|
Junior Member |
|
|
Hi all,
I have 2 tables that doesn't have primary keys. These 2 tables have same number of rows. I want to create a new table from getting some columns from table1 and some columns from table 2. I want to combine first row from table1 and first row from table2.
Below is example
TABLE1
ACOL1 ACOL2 ACOL3
A1 A2 A3
B1 B2 B3
C1 C2 C3
TABLE2
BCOL1 BCOL2 BCOL3
11 12 13
21 22 23
31 32 33
COMBINED_TABLE
ACOL1 BCOL2 BCOL3
A1 12 13
B1 22 23
C1 32 33
I tried below query but no luck. It gives below error:
Query : create table COMBINED_TABLE AS select a.ACOL1, b.BCOL2, b.BCOL3 from (select ACOL1,rownum from TABLE1) a, (select BCOL2, BCOL3, rownum from TABLE2) b WHERE a.rownum = b.rownum
Error : ORA-01747:"invalid user.table.column, table.column, or column specification"
|
|
|
|
|
|
|
Re: CREATE New Table from existing tables [message #600111 is a reply to message #600110] |
Thu, 31 October 2013 18:33 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
BlackSwan wrote on Fri, 01 November 2013 04:45Lalit Kumar B wrote on Thu, 31 October 2013 16:10CTAS(create table as select) :-
select a.ACOL1, b.BCOL2, b.BCOL3 from TABLE1 a, TABLE2 b;
Is Cartesian Product the right answer?
Cartesian product is always a nightmare for any developer. It is OP's fault here. There is no clue to make out what could be the JOIN conditions or a WHERE CLAUSE as a PREDICATE to avoid a Cartesian product. Alas!
|
|
|
Re: CREATE New Table from existing tables [message #600113 is a reply to message #600111] |
Thu, 31 October 2013 18:53 |
cookiemonster
Messages: 13922 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
OP wants a result with the same number of rows as the input tables. Cartesian will not do that.
@arifugurel - You do realize that rows in a table have no inherent order, so what your trying to do would match rows randomly unless you have some other column to order by?
|
|
|
Re: CREATE New Table from existing tables [message #600121 is a reply to message #600113] |
Fri, 01 November 2013 01:20 |
|
Littlefoot
Messages: 21809 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
Here's how it might look like (utilizes order Cookiemonster mentioned):
SQL> with
2 -- table1 and table2 is what you currently have
3 table1 as
4 (select 'A1' col1, 'A2' col2, 'A3' col3 from dual union
5 select 'B1', 'B2', 'B3' from dual union
6 select 'C1', 'C2', 'C3' from dual
7 ),
8 table2 as
9 (select 11 col1, 12 col2, 13 col3 from dual union
10 select 21, 22, 23 from dual union
11 select 31, 32, 33 from dual
12 ),
13 -- first, let's order these rows
14 t1_ordered as
15 (select row_number() over (order by col1) rn,
16 col1, col2, col3
17 from table1
18 ),
19 t2_ordered as
20 (select row_number() over (order by col1) rn,
21 col1, col2, col3
22 from table2
23 )
24 -- now join them on RN
25 select t1.col1, t2.col2, t2.col3
26 from t1_ordered t1,
27 t2_ordered t2
28 where t1.rn = t2.rn
29 order by t1.rn;
CO COL2 COL3
-- ---------- ----------
A1 12 13
B1 22 23
C1 32 33
SQL>
|
|
|