February 18, 2014 at 3:06 am
Hi all,
I have two totally different tables with completely different data fields. Also, there is no common relationship between these two tables. However, I want to pick few data fields from the each table and merge into a new table! Is this even possible? If so how can I do that?
Thanks.
February 18, 2014 at 4:52 am
artistdedigital (2/18/2014)
Hi all,I have two totally different tables with completely different data fields. Also, there is no common relationship between these two tables. However, I want to pick few data fields from the each table and merge into a new table! Is this even possible? If so how can I do that?
Thanks.
Absolutely:
SELECT t1.Col1, t1.Col2, t2.Col3, t2.Col4
INTO Table3
FROM Table1 AS t1
, Table2 AS t2
It will create for you new table (Table3) as nice Cartesian product of Table1 and Table2.
Is it not what you rally looking after? Not surprised at all...
Please check the article from the link at the bottom of my signature. I hope it will help you to explain you issue in a way to attract more relevant answers.
February 18, 2014 at 6:13 am
If you want a one-row-one-row combination you could use a cursor on each table and walk through the rows spitting out a new row with all of the relevant fields from each table. You could also do a row_number() on each set and join on that to get a one-to-one matchup. This is much better because not only is it not RBAR it also gives you at least some control about how to order the numbering.
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
February 19, 2014 at 4:18 am
SELECT * FROM
(
SELECT datafields
FROM t1
UNION ALL
SELECT datafields FROM t2
) AS Anewtable WHERE t1.something = t2.something
Am I right here? I have to do the entire process in three tiers...almost 45 tables to around 20, then that 20 tables to 1 large table...
BTW, I am not actually creating new tables but more like create new views...the goal is to create one single sql query which we can run everyday to create one single table to pick datasets and run a report from...I hope its kind of clear now 🙂 any cool ideas to get this? thanks heaps...
February 19, 2014 at 7:38 am
I thought you first said that there is no relationships between either table...in any case the first suggestion is correct in that it will give you a cartesian product. If I were you I would see if there is any relationship you would derive from both tables because getting a cartesian product could lead to an immense number of rows.
February 19, 2014 at 7:59 am
...I hope its kind of clear now 🙂
...
Not at all. In your first piost you stated that :
I have two totally different tables with completely different data fields.
Which means that you cannot use UNION, as it will require the same set of fields in both parts of union.
Using UNION you can select the common sub-set of the fields if there is one.
Also you said that:
Also, there is no common relationship between these two tables.
Which means that you cannot determine data from which row of Table2 should be selected for some row of Table1. So, the only reasonable suggestion is: you can built cartesian product - nothing else. (Actually, if youn number rows in both tables and join them based on this number, you will get nothing else other than subset of cartesion product, as you will still have no common relation between data of your two tables)
If you really want to get any relevant help, I strongly advised you to provide at least some small sample of your table DDL's and data, with explaining what results you would like to see from samples you posted.
Please read the forum etiquette article from the link at the bottom of my signature...
February 20, 2014 at 2:56 am
You' d better be more specific. For example if you need all rows from T1 plus some rows of T2 which are some way related to T1 it may be type of
SELECT f1,..fn FROM T1
UNION ALL
SELECT g1, .. gn
FROM T2
WHERE EXISTS (SELECT 1 FROM T1 x WHERE x... = T2...)
February 20, 2014 at 4:33 am
Okay…sorry guys for making you all so confused :w00t: I really didn't mean it...
Here is an example –
Select R1,R2,R3
From Database.table1 as table.x
Select C1,C2,C3
From Database.table2 as table.y
Now, I want to merge R1,R2,R3,C1,C2,C3 in a new table called table.z
How can I do that? Sorry, if it’s a very lame question…Thanks to you all for your help 🙂
February 20, 2014 at 4:45 am
Still wonder what you mean merge? Pls, provide sample src data and the result expected.
February 20, 2014 at 4:59 am
Merge means I want to get all the 6 columns(3 on each table) in to a new table...
February 20, 2014 at 5:13 am
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
February 20, 2014 at 5:48 am
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
February 20, 2014 at 5:49 am
serg-52 (2/20/2014)
Then JOIN themselect 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
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
February 21, 2014 at 2:32 am
You can use Merge join available in SSIS package
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply