Solved

SQL Query Join/Combine rows

Posted on 2007-03-21
4
273 Views
Last Modified: 2010-03-20
I am using MS SQL Server 2005 and would like to join/combine ALL the rows in Table A to be in the output query along with all rows from Table B joined with data from Table A.

                   Table A
Addr ID            Address Data
101            101 Main
103            103 Main
104            104 Main
105            105 Main

               Table B
Apt ID            Addr ID            Apt Data
xxA             103            apt A
xxB            103            apt B
xxC            103            apt C

              Desired Query Output
101 Main
103 Main                         <-----  (how do I get this row?)
103 Main       apt A
103 Main       apt B
103 Main       apt C
104 Main
105 Main
0
Comment
Question by:gordonrslater
  • 2
4 Comments
 
LVL 17

Expert Comment

by:HuyBD
ID: 18768966
select [Address Data] from tableA
left join tableB on tableA.[Addr ID]=tableB.[Addr ID]

HuyBD;
0
 
LVL 10

Expert Comment

by:StephenCairns
ID: 18769118
Are you wanting all the records from tableA and all the records from tableA with data from table B?

what your desired output seems to suggest is
select [Address Data] , [Apt Data]
 from tableA
left join tableB on tableA.[Addr ID]=tableB.[Addr ID]
union
select [Address Data] , ""  as [Apt Data]
 from tableA
order by [Address Data]
0
 
LVL 10

Accepted Solution

by:
StephenCairns earned 500 total points
ID: 18769131
actually make that

select [Address Data] , [Apt Data]
 from tableA
inner join tableB on tableA.[Addr ID]=tableB.[Addr ID]
union
select [Address Data] , ""  as [Apt Data]
 from tableA
order by [Address Data]
0
 

Author Comment

by:gordonrslater
ID: 18772980
It worked as requested.  Thanks!!!!
grs
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question