Solved

SQL Query Join/Combine rows

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RESTORE A BACKUP IN SQL 2012 from SQL 2008 9 68
IIF in access query 19 28
sql server query 18 42
Casting is giving error in sql server 3 12
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

820 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