Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 JOIN TABLES WITH 15 COLUMNS EACH

Author  Topic 

ocean
Starting Member

22 Posts

Posted - 2012-05-08 : 11:26:36
Hi there,

I have 2 tables in the same database. Each table has 15 columns, and I need to create a new table in the same database, using the info from the 2 tables that I have in my database.

Those 2 tables they have a connection, they only have an ID_Column as connection

The third table, is a mix of the columns form the other two tables.

Any idea?

Thanks!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-08 : 11:29:47
so your result table (third3) will have all columns from both tables horizontally? or you want data to be merged vertically?

If former it should simply be

SELECT t1.*,t2.*
FROM table1 t1
INNER JOIN table2 t2
ON t2.ID_column = t1.ID_Column


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ocean
Starting Member

22 Posts

Posted - 2012-05-08 : 11:39:08
quote:
Originally posted by visakh16

so your result table (third3) will have all columns from both tables horizontally? or you want data to be merged vertically?

If former it should simply be

SELECT t1.*,t2.*
FROM table1 t1
INNER JOIN table2 t2
ON t2.ID_column = t1.ID_Column



We have more columns with the same name as the title (Id, name an others). So if its possible, the query will have to recognize the columns with the same name and copy the data from one of the two tables. (if not i will do it manually)

I want to merge the columns horizontally, in order to have every single column in the same height, so if we have 15 columns and we merge 4 more, we will have all the 19 columns starting at the same height.

Im sorry for asking, im not a pro of the SQL as you can see, but, in that query :

SELECT t1.*,t2.*
FROM table1 t1
INNER JOIN table2 t2
ON t2.ID_column = t1.ID_Column

* ? what does it means, the column name that i want select from the table?
FROM table1 t1 ? From table 1 (...), I dont understand the part table 1 t1
INNER JOIN table2 t2 ? inner join table2 (...), I dont understand it the part: table2 t2

thanks
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-08 : 11:49:58
So if its possible, the query will have to recognize the columns with the same name and copy the data from one of the two tables.
what do you mean by copy data from one of two tables? Can you show some sample data from table and explain the output you want? see guidance on how to post data below

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

I want to merge the columns horizontally, in order to have every single column in the same height, so if we have 15 columns and we merge 4 more, we will have all the 19 columns starting at the same height.

What height you're talking about here? Are you speaking about displaying data somewhere?

* means include all columns from table

t1,t2 etc are short names given for table called table alias


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ocean
Starting Member

22 Posts

Posted - 2012-05-08 : 12:03:42
Imagine I have this info:

Table F
Id, name, date of birth, eyes colour, address


Table S
Id, country, date of registerer, address


I need a Table X, that takes the info via the ID, if we have the same ID, copy in Table X the following columns:

Table X
Id, name, date of birth, eyes colour, country, date of registerer, address.

Notice, that we have the "address column" in both, so I just need copy that column once and always from Table S

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-08 : 12:13:57
[code]
[code]
SELECT f.Id,
f.name,
f.[date of birth],
f.[eyes colour],
s.country,
s.[date of registerer],
s.address
FROM TableF f
INNER JOIN TableS s
ON s.Id = f.Id
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -