Solved

SQL Syntax Help - Merging 2 tables

Posted on 2011-02-15
14
334 Views
Last Modified: 2012-05-11
Hi All,

Can you help me with some syntax..

I have the following 2 tables which are clearly different other than RegID and RegEID which in some instances they store the same values:

Table1

RegID   Title    Unit
1           Test    2
2            Prep   1
3            Start   1

Table2
RegEID     Capacity    Date
1               1.6             02/01/2009
3                2.2            10/12/2010
5                4.0             01/02/2011

What I need to achieve is I need to return all records from both tables whether RegID and RegEID match or not. If they do match then these records need all show on the same row like so:

RegID/RegEID     Title          Unit             Capacity         Date
1                         Test          2                 1.6                 02/01/2009
2                         Prep          1                 NULL             NULL
3                         Start          1                 2.2                10/12/2010
5                         NULL        NULL           4.0                 01/02/2011


Can anyone provide any guidance on this?

Many thanks,

Rit
0
Comment
Question by:rito1
  • 8
  • 5
14 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 34900905
try

select *
from table1 t1 full join on table2 t2 on t1.regid=t2.regeid
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34900914
oops a tyoe above, should read as:

select *
from table1 t1 full join table2 t2 on t1.regid=t2.regeid
0
 
LVL 1

Author Comment

by:rito1
ID: 34900923
Thanks HainKurt. I am just trying it now.

Rit
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34900931
if you want only one id


select ISNULL(t1.id,t2.id) as RegID, t1.*, t2.*
from table1 t1 full join table2 t2 on t1.regid=t2.regeid
order by 1
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34900949
of course id should replace with regid/regeid

select ISNULL(t1.regid,t2.regeid) as RegID, t1.*, t2.*
from table1 t1 full join table2 t2 on t1.regid=t2.regeid
order by 1
0
 
LVL 1

Author Comment

by:rito1
ID: 34900992
select *
from table1 t1 full join table2 t2 on t1.regid=t2.regeid

I currently get syntax error on the FULL keyword. Any ideas?... Just to confirm, I am running this SQL in Access 2007.

Thanks,

Rit
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 34901049
access does not support full join... so we need some unions...

try this

select * from table1 t1 left join table2 t2 on t1.regid=t2.regeid
union
select * from table2 t2 left join table1 t1 on t1.regid=t2.regeid

then we can work on combining the id's into one column...
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 51

Expert Comment

by:HainKurt
ID: 34901053
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34901066
where does the sql server come into the process then?

please remoive the topic area if it doesn't apply
0
 
LVL 1

Author Comment

by:rito1
ID: 34901149
Sorry about the inclusion of the SQL Server topic. Yes this is incorrect and should be for Access. I cant seem to remove it.

Rit
0
 
LVL 1

Author Comment

by:rito1
ID: 34901158
HainKurt,

I am just trying the UNION JOIN theory now.

Rit
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34901210
try this one too

select * from table1 t1 left join table2 t2 on t1.regid=t2.regeid
union
select * from table1 t1 right join table2 t2 on t1.regid=t2.regeid WHERE t1.RegID is null


0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34901248
or filtering the cross join if the tables are relatively small

select * from table1 t1, table2 t2 where (t1.RegID=t2.RegEID or t1.RegID is null or t2.RegEID is null)
0
 
LVL 1

Author Closing Comment

by:rito1
ID: 34905073
Brilliant. Thanks for your help.

Rit
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

948 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

16 Experts available now in Live!

Get 1:1 Help Now