Home » SQL & PL/SQL » SQL & PL/SQL » Insert into table from another table based on joining column (Oracle 10)
Insert into table from another table based on joining column [message #643852] Tue, 20 October 2015 05:32 Go to next message
pratik4891
Messages: 73
Registered: February 2011
Member
Please find my requirement below

Below are the tables -

create table AA
as
select 1 as id ,'A' as item_code from dual
union
select 2 as id ,'B' as item_code from dual
union
select 3 as id ,'C' as item_code from dual

create table BB
as
select 1 as id ,'AAA item' as item_description from dual
union
select 2 as id ,'BBB item' as item_description from dual
union
select 3 as id ,'CCC item' as item_description from dual


I need to union these tables like based on id rows will be inserted in the final table

Needed Output 

Id 	Output
1	A
1	AAA item
2	B
2	BBB item
3	C
3	CCC item


like for id 1 two rows will be together
Can anyone please suggest how to approach this solution ?

Thanks,

Re: Insert into table from another table based on joining column [message #643854 is a reply to message #643852] Tue, 20 October 2015 05:41 Go to previous messageGo to next message
John Watson
Messages: 9003
Registered: January 2010
Location: Global Village
Senior Member
I would use a UNION ALL compound query, and in the column projection list for each table include a constant that identifies from which table the row comes. Then you can ORDER BY the id and the constant.
Re: Insert into table from another table based on joining column [message #643855 is a reply to message #643852] Tue, 20 October 2015 05:44 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
You do realize that data in tables is not stored in any particular order even if you insert it in a particular order?
If you want to be able to query the data from the third table in that particular order then you're going to need an extra column in the third table to show what the order is.
Re: Insert into table from another table based on joining column [message #643867 is a reply to message #643855] Tue, 20 October 2015 07:47 Go to previous messageGo to next message
pratik4891
Messages: 73
Registered: February 2011
Member
Thanks to both of you ,it seems I found a solution
Re: Insert into table from another table based on joining column [message #643869 is a reply to message #643867] Tue, 20 October 2015 07:49 Go to previous message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

So post it to help future readers.

Previous Topic: Filling values
Next Topic: Select with Time Range
Goto Forum:
  


Current Time: Mon Jul 13 20:37:35 CDT 2026