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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Microsoft 2005 SQL Query solution needed

Author  Topic 

jszulc
Yak Posting Veteran

66 Posts

Posted - 2008-09-25 : 18:39:03
I have too tables that look like this:

TABLE 1
Address City Server State SID
7002 Prospect Place Northeast Albuquerque ABQNV001 NM F020181
7002 Prospect Place Northeast Albuquerque ABQNV001 NM F001949
7002 Prospect Place Northeast Albuquerque ABQNV001 NM N074529
7002 Prospect Place Northeast Albuquerque ABQNV001 NM D146757
7002 Prospect Place Northeast Albuquerque ABQNV001 NM E046197
7002 Prospect Place Northeast Albuquerque ABQNV001 NM E047216
7002Prospect Place Northeast Albuquerque ABQNV001 NM O002832
7002 Prospect Place Northeast Albuquerque ABQNV001 NM E053340
3400 Murray Street Alexandria AEXNV001 LA E033698
3400 Murray Street Alexandria AEXNV001 LA E034262
3400 Murray Street Alexandria AEXNV001 LA E035467


and TABLE2:
Address City Server State SID CODE
7002 Prospect Place Northeast Albuquerque ABQNV001 NM F020181 PD
7002 Prospect Place Northeast Albuquerque ABQNV001 NM F020181 PD
7002 Prospect Place Northeast Albuquerque ABQNV001 NM F020181 PD
7002 Prospect Place Northeast Albuquerque ABQNV001 NM F001949 ZX
7002 Prospect Place Northeast Albuquerque ABQNV001 NM F001949 ZX
7002 Prospect Place Northeast Albuquerque ABQNV001 NM F001949 ZX
7002 Prospect Place Northeast Albuquerque ABQNV001 NM N074529 UV
7002 Prospect Place Northeast Albuquerque ABQNV001 NM D146757 MO
7002 Prospect Place Northeast Albuquerque ABQNV001 NM D146757 MO
7002 Prospect Place Northeast Albuquerque ABQNV001 NM D146757 MO
3400 Murray Street Alexandria AEXNV001 LA E033698 ZE
3400 Murray Street Alexandria AEXNV001 LA E033698 ZE
3400 Murray Street Alexandria AEXNV001 LA E033698 ZE
3400 Murray Street Alexandria AEXNV001 LA E034262 FT
3400 Murray Street Alexandria AEXNV001 LA E035467 GA

I would like to display all 11 records (ONLY!!!) from TABLE1 and match them to TABLE2. Whener there is a match display CODE column. SO the output woul look like this:

Address City Server State SID CODE
7002 Prospect Place Northeast Albuquerque ABQNV001 NM F020181 PD
7002 Prospect Place Northeast Albuquerque ABQNV001 NM F001949 ZX
7002 Prospect Place Northeast Albuquerque ABQNV001 NM N074529 UV
7002 Prospect Place Northeast Albuquerque ABQNV001 NM D146757 MO
7002 Prospect Place Northeast Albuquerque ABQNV001 NM E046197
7002 Prospect Place Northeast Albuquerque ABQNV001 NM E047216
7002Prospect Place Northeast Albuquerque ABQNV001 NM O002832
7002 Prospect Place Northeast Albuquerque ABQNV001 NM E053340
3400 Murray Street Alexandria AEXNV001 LA E033698 ZE
3400 Murray Street Alexandria AEXNV001 LA E034262 FT
3400 Murray Street Alexandria AEXNV001 LA E035467 GA

as you can see I did not change the # of rows from table 1. If there is no match, the query would return no match. How would I accomplish this. I was thinking about using SID column. Thank you very much.

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-09-25 : 18:55:04
Select tb1.Address,tb1.City,tb1.Server,
tb1.State,tb1.SID,coalesce(tb2.Code,'')as Code
from table1 tb1 left outer join table2 tb2
on tb1.Address =tb2.Address and tb1.City = tb2.City and tb1.Server=tb2.Server and tb1.State =tb2.State and tb1.SID = tb2.SID
Go to Top of Page

jszulc
Yak Posting Veteran

66 Posts

Posted - 2008-09-25 : 22:08:29
Thank you. Problem solved!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-26 : 01:28:14
You should be taking only distinct records from TABLE2 for joining else you will get lot of duplicates in result.
Go to Top of Page
   

- Advertisement -