SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 INNER JOIN
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jfm
Posting Yak Master

134 Posts

Posted - 05/10/2012 :  08:26:16  Show Profile  Reply with Quote
Hi guys,

Im trying to create a unique table (FinalTable), using Table1 and Table2 from the same database.

FinalTable: I need to have the info from Table1 and 2. Not all of it. Imagine that I have the following Columns in each table:

Eyes_Colour Name_Student


In my Final_Table I need all the Eyes_Colour and Name_Student that are the same in Table1 and Table2. So the info that I will have it will be, all those students that have the same: Name_Student and Eyes_Colour, in both tables (Table1 and 2)

The query that im using its:

SELECT Eyes_Colour, Name_Student INTO Final_Table FROM Table1
INNER JOIN Table2
ON Table1.Name_Student = Table2.Name_Student AND Table2.Eyes_Colour = Table1.Eyes_Colour

msg 209, Level 16, State 1, Line 2
Ambiguous column name 'Name_Student'
msg 209, Level 16, State 1, Line 2
Ambiguous column name 'Eyes_Colour'

Any idea?

Thanks

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/10/2012 :  08:39:35  Show Profile  Reply with Quote

SELECT Eyes_Colour, Table1.Name_Student INTO Final_Table FROM Table1
INNER JOIN Table2
ON Table1.Name_Student = Table2.Name_Student AND Table2.Eyes_Colour = Table1.Eyes_Colour



KH
Time is always against us

Go to Top of Page

ocean
Starting Member

22 Posts

Posted - 05/10/2012 :  08:51:30  Show Profile  Reply with Quote
Still not working ...

I have the same msg 209, level 16, state 1, line 1

Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/10/2012 :  09:02:01  Show Profile  Reply with Quote
add the prefix to Eyes_colour also


KH
Time is always against us

Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 05/10/2012 :  09:03:42  Show Profile  Reply with Quote

SELECT Table2.Eyes_Colour, Table1.Name_Student INTO Final_Table FROM Table1
INNER JOIN Table2
ON Table1.Name_Student = Table2.Name_Student AND Table2.Eyes_Colour = Table1.Eyes_Colour


Is working that way, using both tables in SELECT.

But the info that I have, is repeating, I dont know if its because, is the number of times that they match, and thats why I have the same rows depending on the student or why.

How Im able just, to have as output, the Name and the Colour one time?

Using COUNT. Where do I have to use COUNT in the query?

Thanks
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/10/2012 :  09:06:31  Show Profile  Reply with Quote
it is repeating because for each Name_Student & Eyes_Colour in table1 there are more than 1 such records in table2.

You can use DISTINCT to only return the distinct value


KH
Time is always against us

Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 05/10/2012 :  09:26:57  Show Profile  Reply with Quote
Perfect!

Thanks
Go to Top of Page

vijays3
Constraint Violating Yak Guru

India
311 Posts

Posted - 05/10/2012 :  12:22:41  Show Profile  Reply with Quote
If you want to combine the data from both the table .You can use the Union to retrive distinct records from both table

select * into tab3 from
( select Eyes_Colour,Name_Student from tab1
union
select Eyes_Colour,Name_Student from tab2) A
Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 05/10/2012 :  12:42:05  Show Profile  Reply with Quote
Thats perfect!!



quote:
Originally posted by vijays3

If you want to combine the data from both the table .You can use the Union to retrive distinct records from both table

select * into tab3 from
( select Eyes_Colour,Name_Student from tab1
union
select Eyes_Colour,Name_Student from tab2) A

Go to Top of Page

vijays3
Constraint Violating Yak Guru

India
311 Posts

Posted - 05/10/2012 :  17:53:56  Show Profile  Reply with Quote
Welcome
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000