Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Merge two tables into one. Expand / Collapse
Author
Message
Posted Thursday, February 20, 2014 5:13 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 6:56 AM
Points: 162, Visits: 355
Then JOIN them

select x.R1,x.R2,x.R3, y.C1,y.C2,y.C3
from Database.table1 as x
join Database.table2 as y

to get all possible combinations aka cartesian product.
Restrict combinations using WHERE clause

http://technet.microsoft.com/en-us/library/ms191517(v=sql.105).aspx
Post #1543419
Posted Thursday, February 20, 2014 5:48 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:55 AM
Points: 2,873, Visits: 5,185
artistdedigital (2/20/2014)
Merge means I want to get all the 6 columns(3 on each table) in to a new table...



Select R1,R2,R3
From Database.table1 as table.x


Select C1,C2,C3
From Database.table2 as table.y


It may surprise you, but tables are not just set of columns...
Your select will or may return some rows!

What about if select from your table Database.table1 returns as twice as many rows as select from Database.table2?

That do you execpt to be result of the merge? That is why I have asked you to provide the example of expected results based on a sample of data you have. Until you do that, you are no going to get relevant help, as it's imposible to see what is inside of your head (I've lost my crystal ball to do so)...

The maximum what I can tell you right now, that to have resultset containig 6 columns from the both selects you have you need to JOIN your table somehow. If there is nothing links rows of these two tables, then you have two options: 1. Cartesian product and 2. UNION ALL.

You already been shown examples of option #1:
To do union in your case you can try this:

INSERT [YourNewTable]
SELECT R1,R2,R3, NULL, NULL, NULL
FROM Database.table1 as table.x
UNION ALL
Select NULL, NULL, NULL, C1, C2, C3
FROM Database.table2 as table.y






_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1543437
Posted Thursday, February 20, 2014 5:49 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 4:44 AM
Points: 6,792, Visits: 14,005
serg-52 (2/20/2014)
Then JOIN them

select x.R1,x.R2,x.R3, y.C1,y.C2,y.C3
from Database.table1 as x
join Database.table2 as y

to get all possible combinations aka cartesian product.
Restrict combinations using WHERE clause

http://technet.microsoft.com/en-us/library/ms191517(v=sql.105).aspx


On its own, join will raise an error.

select x.R1,x.R2,x.R3, y.C1,y.C2,y.C3
from Database.table1 as x
CROSS JOIN Database.table2 as y


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1543439
Posted Friday, February 21, 2014 2:32 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, May 21, 2014 4:09 AM
Points: 37, Visits: 142
You can use Merge join available in SSIS package
Post #1543872
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse