Solved

SQL Query Join/Combine rows

Posted on 2007-03-21
4
271 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
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.

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now