Solved

SQL Query Join/Combine rows

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

743 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