Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Unite 2 tables into 1

Status
Not open for further replies.

microsky

MIS
May 7, 2001
33
I have a question about to unite two tables into one, the details as following:
Source Table A:
col1 col2
a x
a xx
Source Table B:
b y
b yy

And I want to get the Target Table C as:
a x b y
a xx b yy

Any one hnow how to get the target table?
Thanks,
WangYun


 
sorry , the Source table B should be:
col1 col2
a y
a yy
and the target table C should be:
col1 col2 col3
a x y
a xx yy
 
If you know that for every possible value in col1, there are the same number of rows in tables a and b, you can do something like this:
Code:
 SELECT aa.col1, aa.col2, bb.col2 as col3
   FROM (SELECT col1, col2, ROWNUM AS rank
           FROM a
          ORDER BY col1,col2) aa,
         (SELECT col1, col2, ROWNUM AS rank
            FROM b
           ORDER BY col1, col2) bb
 WHERE aa.rank = bb.rank;
 
I am giving a straight forward way to solv this problem:

select * from (select a.col1,a.col2,b.col2 as col3
from a,b
where a.col1 = b.col1)
where length(col2) = length(col3);
 
And even more straightforward :)

select a.col1,a.col2,b.col2
from a,b
where a.col1 = b.col1
and length(a.col2) = length(b.col2);
Regards, Dima
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top