how to join tables sql server

Posted by Rick on Stack Overflow See other posts from Stack Overflow or by Rick
Published on 2012-03-26T17:12:09Z Indexed on 2012/03/26 17:29 UTC
Read the original article Hit count: 338

Filed under:

Im having some trouble with joining two tables. This is what my two tables look like:

Table 1

Customer_ID   CustomerName   Add. 
1000          John Smith
1001          Mike Coles
1002          Sam Carter

Table 2

Sensor_ID     Location      Temp    CustIDFK
1000          NY            70
1002          NY            70
1000          ...           ...
1001
1001
1002

Desired:

Sensor_ID     Location      Temp    CustIDFK
1000          NY            70      John Smith
1002          NY            70      Sam Carter
1000          ...           ...     John Smith
1001                                Mike Coles
1001
1002

I have made Customer_ID from table 1 my primary key, created custIDFK in table 2 and set that as my foreign key. I am really new to sql server so I am still having trouble with the whole relationship piece of it. My goal is to match one customer_ID with one Sensor_ID. The problem is that the table 2 does not have "unique IDs" since they repeat so I cant set that to my foreign key. I know I will have to do either an inner join or outer join, I just dont know how to link the sensor id with customer one. I was thinking of giving my sensor_ID a unique ID but the data that is being inserted into table 2 is coming from another program. Any suggestions?

© Stack Overflow or respective owner

Related posts about sql-server